首页  >  文章  >  后端开发  >  php之curl get post请求用法实例分享

php之curl get post请求用法实例分享

黄舟
黄舟原创
2017-10-20 09:23:142975浏览

   一、CURL小结

         个人将归纳curl请求总结成三步

         1、创建curl 句柄(curl_init),并设置参数(curl_setopt)(打开冰箱)

         2、执行请求(curl_exec),处理返回的数据 (把大象塞进去)

         3、关闭curl(curl_close),释放所有资源(关上冰箱)

         其实如果代码看起来比较复杂,复杂的地方可能就是在处理返回数据的逻辑。

二、CURL_SETOPT

        故名思议,SetOption 设置参数,其中囊括的参数较多,这里只是简单提取常用的几个,如需查看更多参数,点击这里,常见的设置UA、Cookie、https等


bool curl_setopt          (   , int  ,  "User-Agent: ""Referer: " 禁止 cURL 验证对等证书(peer'

     如需返回的Header头,自行添加 


curl_setopt($curl, CURLOPT_HEADER, 1);

     判断返回的状态码:


curl_getinfo(, CURLINFO_HTTP_CODE)
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')

     简单版的GET请求如下,下面以请求百度为例,只设置了最基本的属性:


 =, CURLOPT_URL, 'http://www.baidu.com', CURLOPT_HEADER, 1, CURLOPT_RETURNTRANSFER, 1 = curl_exec(();
?>

       稍微复杂的设置了UA、Cookie等,https请求中才需要只用的SSL证书校验,http请求中可不用,如果需要请求有规律的地址,类似https://example.com/?id=$i,修改for循环即可。


<?php
class getRequest
{    
const sUA = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&#39;;    
const sURL = &#39;https://www.baidu.com&#39;;    
const sCookie = &#39;fake if you want&#39;;    
function vInitRequest()
    {        $curl = curl_init();

        curl_setopt($curl, CURLOPT_HEADER, self::sUA);
        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        
        /*
         * ssl check,use for https url         
         */
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);//        
        for ($iId = 1; $iId < 1000; $iId++) {
//            $sURL = self::sURL.$iId;
        curl_setopt($curl, CURLOPT_URL, self::sURL);        
        $this->sExecRequest($curl);//        }    }    function sExecRequest($curl)
    {        $sRet = curl_exec($curl);        print_r($sRet);        /**
         * handle your response
         * stripos or preg         */
        curl_close($curl);
    }
}$foo = new getRequest();$foo->vInitRequest();?>

 

三、分离Response里面的 Header和Body

      首先要显示Header信息需要设置,如下设置即可取到header和body,当然还有其他方法大同小异


curl_setopt($curl, CURLOPT_HEADER, 1);list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);

      完整代码:


<?php
class getRequest
{    
const sUA = &#39;Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)&#39;;    
const sURL = &#39;https://www.baidu.com&#39;;    
const sCookie = &#39;fake if you want&#39;;    
function vInitRequest()
    {        
    $curl = curl_init();        
    $i = 0;
        curl_setopt($curl, CURLOPT_HEADER, self::sUA);
        curl_setopt($curl, CURLOPT_COOKIE, self::sCookie);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($curl, CURLOPT_URL, self::sURL);        
        $this->sExecRequest($curl);
    }    function sExecRequest($curl)
    {        $sRet = curl_exec($curl);        
    // if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == &#39;200&#39;) {
            list($sHeader, $sBody) = explode("\r\n\r\n", $sRet, 2);        
            // }
            print_r($sHeader);            
            print_r($sBody);        
            // curl_close($curl);    }
}$foo = new getRequest();$foo->vInitRequest();?>

 

四、POST请求

      POST请求无非比上述Get请求多设置了两个参数。

      1、嘿,我要用POST提交数据了。

      2、我POST的数据的内容


curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(&#39;user&#39;=>&#39;test&#39;));

      简单版如下:      


<?php$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, &#39;http://www.baidu.com&#39;);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);$aPostData = array(  &#39;username&#39; => &#39;test&#39;,
   .....);
curl_setopt($curl, CURLOPT_POSTFIELDS, $aPostData);$sData = curl_exec($curl);
curl_close($curl);var_dump($sData);?>

以上是php之curl get post请求用法实例分享的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn