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