PHP中使用CURL扩展实现HTPP请求发送

 1 /**
 2  * PHP发送异步请求
 3  * @author ThinkPHP
 4  * @date 2012-12-19
 5  * @param string $url 请求地址
 6  * @param array $param 请求参数
 7  * @param string $httpMethod 请求方法GET或者POST
 8  * @return array
 9  * @link http://www.thinkphp.cn/code/71.html
10  */
11 function makeRequest($url, $param, $httpMethod = 'GET') {
12     $oCurl = curl_init();
13     if (stripos($url, "https://") !== FALSE) {
14         curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
15         curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
16     }
17     if ($httpMethod == 'GET') {
18         curl_setopt($oCurl, CURLOPT_URL, $url . "?" . http_build_query($param));
19         curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
20     } else {
21         curl_setopt($oCurl, CURLOPT_URL, $url);
22         curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
23         curl_setopt($oCurl, CURLOPT_POST, 1);
24         curl_setopt($oCurl, CURLOPT_POSTFIELDS, http_build_query($param));
25     }
26     $sContent = curl_exec($oCurl);
27     $aStatus = curl_getinfo($oCurl);
28     curl_close($oCurl);
29     if (intval($aStatus["http_code"]) == 200) {
30         return array('http_status'=>$aStatus,'data'=>$sContent);
31     } else {
32         return array('http_status'=>$aStatus,'data'=>false);
33     }
34 }

 

本方法参考:http://www.thinkphp.cn/code/71.html,并略作调整。

posted @ 2014-11-06 20:18  sungx2009  阅读(216)  评论(0)    收藏  举报