curl常用设置-涉及超时相关

来源:互联网 发布:ping带端口的ip 编辑:程序博客网 时间:2024/06/06 18:29
curl_easy_setopt( curl, CURLOPT_VERBOSE, 1L ); //在屏幕打印请求连接过程和返回http数据curl_easy_setopt( curl, CURLOPT_TIMEOUT, 10 );//接收数据时超时设置,如果10秒内数据未接收完,直接退出curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1); // 以下3个为重定向设置curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //返回的头部中有Location(一般直接请求的url没找到),则继续请求Location对应的数据 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深curl_easy_setopt( curl, CURLOPT_CONNECTTIMEOUT, 3 );//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了

转自:http://blog.csdn.net/lizhi200404520/article/details/7369658

==========================================

以及下面实际运用相关代码段:

foreach ($url_array as $url) {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_HEADER, 0);        curl_setopt($ch, CURLOPT_TIMEOUT, 50);        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);        // 使用自动跳转         curl_setopt($ch, CURLOPT_MAXREDIRS, 7);        curl_setopt($ch, CURLOPT_REFERER, $url);        curl_setopt($ch, CURLOPT_ENCODING, "gzip");        if ($pCookie != "") {            curl_setopt($ch, CURLOPT_COOKIEFILE, $pCookie); // 读取上面所储存的Cookie信息         }        curl_multi_add_handle($mh, $ch); // 把 curl resource 放进 multi curl handler 里        $handle[$i++] = $ch;    }

摘自:http://bbs.csdn.net/topics/380152499

0 0
原创粉丝点击