PHP 使用 CURL 发送HTTP请求

  1.     function http($url, $method, $postfields = NULL, $headers = array()) {  
  2.         $this->http_info = array();  
  3.         $ci = curl_init();  
  4.         /* Curl settings */  
  5.         curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//让cURL自己判断使用哪个版本  
  6.         curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);//在HTTP请求中包含一个"User-Agent: "头的字符串。  
  7.         curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);//在发起连接前等待的时间,如果设置为0,则无限等待  
  8.         curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);//设置cURL允许执行的最长秒数  
  9.         curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);//返回原生的(Raw)输出  
  10.         curl_setopt($ci, CURLOPT_ENCODING, "");//HTTP请求头中"Accept-Encoding: "的值。支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",请求头会发送所有支持的编码类型。  
  11.         curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);//禁用后cURL将终止从服务端进行验证  
  12.         curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));//第一个是cURL的资源句柄,第二个是输出的header数据  
  13.         curl_setopt($ci, CURLOPT_HEADER, FALSE);//启用时会将头文件的信息作为数据流输出  
  14.   
  15.         switch ($method) {  
  16.             case 'POST':  
  17.                 curl_setopt($ci, CURLOPT_POST, TRUE);  
  18.                 if (!empty($postfields)) {  
  19.                     curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);  
  20.                     $this->postdata = $postfields;  
  21.                 }  
  22.                 break;  
  23.             case 'DELETE':  
  24.                 curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');  
  25.                 if (!empty($postfields)) {  
  26.                     $url = "{$url}?{$postfields}";  
  27.                 }  
  28.         }  
  29.   
  30.         if ( isset($this->access_token) && $this->access_token )  
  31.             $headers[] = "Authorization: OAuth2 ".$this->access_token;  
  32.   
  33.         $headers[] = "API-RemoteIP: " . $_SERVER['REMOTE_ADDR'];  
  34.         curl_setopt($ci, CURLOPT_URL, $url );  
  35.         curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );  
  36.         curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );  
  37.   
  38.         $response = curl_exec($ci);  
  39.         $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);  
  40.         $this->http_info = array_merge($this->http_info, curl_getinfo($ci));  
  41.         $this->url = $url;  
  42.   
  43.         if ($this->debug) {  
  44.             echo "=====post data======\r\n";  
  45.             var_dump($postfields);  
  46.   
  47.             echo '=====info====='."\r\n";  
  48.             print_r( curl_getinfo($ci) );  
  49.   
  50.             echo '=====$response====='."\r\n";  
  51.             print_r( $response );  
  52.         }  
  53.         curl_close ($ci);  
  54.         return $response;  
  55.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值