【问题标题】:cURL returns 404 while the page is found in browser在浏览器中找到页面时,cURL 返回 404
【发布时间】:2013-07-02 20:17:11
【问题描述】:

stackoverflow 上已经有类似的问题,但他们的解决方案都没有为我工作。我正在尝试使用 cURL 在 LoveIt.com 上抓取一个页面,但它返回一个 404 错误,而 url 在浏览器中工作正常:

        $url = 'http://loveit.com/loves/P0D1jlFaIOzzZfZqj_bY3KV';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        curl_setopt ($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_REFERER,'http://loveit.com/');

这是我收到的标题:

数组 ( [url] => http://loveit.com/loves/P0D1jlFaIOzzZfZqj_bY3KV [content_type] => text/html; charset=utf-8 [http_code] => 404 [header_size] => 667 [request_size] => 172 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.320466 [namelookup_time] => 0.000326 [connect_time] => 0.119046 [pretransfer_time] => 0.119089 [size_upload] => 0 [size_download] => 499 [speed_download] => 1557 [speed_upload] => 0 [download_content_length] => 499 [upload_content_length] => 0 [starttransfer_time] => 0.320438 [redirect_time] => 0 [certinfo] => Array () [primary_ip] => - -- [primary_port] => 80 [local_ip] => --- [local_port] => 53837 [redirect_url] => )

我读到有些网站有针对此类脚本的保护措施;我确实测试了一些建议的解决方案,但没有一个对我有用 (CURLOPT_USERAGENT,CURLOPT_REFERER...)

对这里发生的事情有任何想法吗?

我想备份我的 LoveIt 帐户,这就是我做这个的原因(没有导出功能,也没有来自 LoveIt.com 的关于网站健康状况的回复)

【问题讨论】:

  • 您是否正在完全重新创建浏览器环境?例如让 curl 发送浏览器可能的任何 cookie?推荐人检查?

标签: php curl


【解决方案1】:

我快速检查了启用 LiveHeaders 的上述页面,我注意到设置了一堆 cookie。我怀疑,因为它不是“正常”的 url,所以你需要在被重定向的同时传递这些 cookie,否则你最终会被 404 踢出。在你的 cURL 实例开始时使用CURLOPT_COOKIEJAR。见:http://php.net/manual/pl/function.curl-setopt.php

【讨论】:

    【解决方案2】:

    我刚刚在一个网站上遇到了类似的问题。就我而言,他们希望设置一个 USER_AGENT,因此将来遇到此问题的任何人也应该检查一下。

    【讨论】:

      【解决方案3】:

      您不需要通过 chrome 保存 cookie 文件。

      您可以创建一个函数来获取此 cookie,然后重复使用它。

      喜欢:

      <?php
      
      error_reporting(E_ALL);
      
      Class Crawler{
      
         var $cookie;
         var $http_response;
         var $user_agent;
      
         function __construct($cookie){
             $this->cookie     = (string) $cookie;
             $this->user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0'; 
         }
      
         function get($url){
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $this->url);
             curl_setopt($ch, CURLOPT_NOBODY, 1);
             curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
             // Here we create the file with cookies
             curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
             $this->http_response = curl_exec($ch);
         }
      
         function get_with_cookies($url){
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_NOBODY, 1);
             curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
             curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
      
             // Here we can re-use the cookie file keeping the save of the cookies 
             curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
             $this->http_response = curl_exec($ch);
          }
      }
      
      $crawler = new Crawler('cookie_file_name');
      // Creating cookie file
      $crawler->get('uri');
      // Request with the cookies
      $crawler->get_with_cookies('uri');
      

      问候。

      【讨论】:

        【解决方案4】:

        感谢您的回答,所以我确实访问了该页面,将 cookie 保存在我使用 NOT CURLOPT_COOKIEJAR 而是用于选项 CURLOPT_COOKIEFILE 的 cookies.txt 文件中(使用 chrome extenson cookie.txt 导出) .

        $cookiefile = './cookie.txt';
        
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
        

        现在它可以工作了!感谢您的反馈,它真的很有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-28
          • 2019-02-07
          相关资源
          最近更新 更多