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;
    }
复制代码
复制代码
posted @   阿风小子  阅读(352)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示