【问题标题】:Script Cannot Continue After curl_exec on Certain URLs在某些 URL 上执行 curl_exec 后脚本无法继续
【发布时间】:2017-07-14 14:29:46
【问题描述】:

我目前正在对 URL 列表运行 cURL,如下面的代码所示。问题是在 URL 的特定主机上运行它时,脚本不会超过 curl_exec() 行。我知道这个过程是可行的,因为我已经在数千个 URL 上成功运行了它,似乎只有一个主机会导致这个问题。出于隐私原因,我不允许专门透露此 URL。我的代码的精简版如下:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, 'www.google.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent['safari']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$url_result = curl_exec($ch);
echo "Will not be printed";
if(curl_error($ch)){
    echo "Still will not be printed";
}
curl_close($ch);

假设 $url 是一个字符串,表示要执行的 URL,$useragent['safari'] 是一个表示 Safari 浏览器用户代理的字符串。

我检查了我的 apache 错误日志和应该打印错误的日志文件,两个日志中都没有任何内容。我还在浏览器中手动输入了此 URL,并成功导航到并加载了该页面。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    试试这个,好像差不多,在这里找到Even CURL function can't scrape some urls

    class Curl
    {       
    
    public $cookieJar = "";
    
    public function __construct($cookieJarFile = 'cookies.txt') {
        $this->cookieJar = $cookieJarFile;
    }
    
    function setup()
    {
    
    
        $header = array();
        $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
        $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] =  "Cache-Control: max-age=0";
        $header[] =  "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; // browsers keep this blank.
    
    
        curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7');
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($this->curl,CURLOPT_COOKIEJAR, $cookieJar); 
        curl_setopt($this->curl,CURLOPT_COOKIEFILE, $cookieJar);
        curl_setopt($this->curl,CURLOPT_AUTOREFERER, true);
        curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);  
    }
    
    
    function get($url)
    { 
        $this->curl = curl_init($url);
        $this->setup();
    
        return $this->request();
    }
    
    function getAll($reg,$str)
    {
        preg_match_all($reg,$str,$matches);
        return $matches[1];
    }
    
    function postForm($url, $fields, $referer='')
    {
        $this->curl = curl_init($url);
        $this->setup();
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_REFERER, $referer);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields);
        return $this->request();
    }
    
    function getInfo($info)
    {
        $info = ($info == 'lasturl') ? curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL) : curl_getinfo($this->curl, $info);
        return $info;
    }
    
    function request()
    {
        return curl_exec($this->curl);
    }
    }
    {
    $curl = new Curl();
    $html = $curl->get("http://www.thefancy.com");
    echo "$html";
    }
    
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 2013-07-28
      • 2017-06-14
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多