当前位置:Gxlcms > PHP教程 > php中curlgetpost请求解析

php中curlgetpost请求解析

时间:2021-07-01 10:21:17 帮助过:15人阅读

一、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'

下面以请求百度为例,需要使用自行设置URL、Ua、Cookie等,https请求中才需要只用的SSL证书校验,http请求中可不用,如果需要请求有规律的地址,类似example.com/?id=$i,修改for循环即可。

<?phpclass getRequest
{    const sUA = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';    const sURL = 'https://www.baidu.com';    const sCookie = 'fake if you want';    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();
?>

以上就是php中curl get post请求解析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行