curl經常使用設置-涉及超時相關

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/7369658html

==========================================curl

以及下面實際運用相關代碼段:ide

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/380152499url

相關文章
相關標籤/搜索