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
==========================================
以及下面实际运用相关代码段:
![复制代码](https://common.cnblogs.com/images/copycode.gif)
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; }
![复制代码](https://common.cnblogs.com/images/copycode.gif)