コード例 #1
0
ファイル: Guard.php プロジェクト: Harren/guard
 /**
  * Method  _sendHttpRequest
  * 发送http请求
  *
  * @author yangyang3
  * @static
  *
  * @param       $method
  * @param       $url
  * @param null  $data
  * @param array $header
  *
  * @return mixed
  */
 private static function _sendHttpRequest($method, $url, $data = null, $header = array())
 {
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::$_connect_timeout);
     curl_setopt($curl, CURLOPT_TIMEOUT, self::$_timeout);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_ENCODING, '');
     curl_setopt($curl, CURLOPT_HEADER, false);
     $method = strtoupper($method);
     if ('GET' === $method) {
         if ($data !== null) {
             if (strpos($url, '?')) {
                 $url .= '&';
             } else {
                 $url .= '?';
             }
             $url .= http_build_query($data);
         }
     } elseif ('POST' === $method) {
         curl_setopt($curl, CURLOPT_POST, true);
         if (!empty($data)) {
             if (is_string($data)) {
                 curl_setopt($curl, CURLOPT_POSTFIELDS, "data=" . $data);
             } else {
                 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
             }
         }
     }
     if (null !== $header) {
         curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLINFO_HEADER_OUT, true);
     $response = curl_exec($curl);
     self::$_http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     curl_close($curl);
     return $response;
 }