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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM