网站推广.NET

网站推广.NET

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

来源:互联网

   一、curl小结

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

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

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

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

立即学习“PHP免费学习笔记(深入)”;

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

二、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循环即可。


<?phpclass 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);

完整代码:


<?phpclass 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);?>
curl post