【问题标题】:Is this site blocking/ignoring my HTTP requests?该站点是否阻止/忽略了我的 HTTP 请求?
【发布时间】:2011-07-22 22:29:38
【问题描述】:

当我将 cURL 与代理一起使用时,我只能从站点获取数据。没有代理的 cURL 和 file_get_contents() 不返回任何内容(cURL HTTP 代码“0”和 curl_error() Empty reply from server)。我可以在没有代理的情况下获取其他网站。

除了被屏蔽之外,还有其他解释为什么我只能通过代理访问这个网站吗?

【问题讨论】:

  • echo curl_error($curl); 的输出是什么?
  • 用 curl_error() 输出更新

标签: php http curl


【解决方案1】:

您是否在 cURL 中设置了用户代理?如果您的 USER AGENT 未设置或您的 HTTP 请求看起来可疑,有时网站会阻止您。

在 PHP 中设置您的用户代理:

curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

【讨论】:

    【解决方案2】:

    这是来自你的工作场所还​​是什么?许多公司在共享 PHP 安装时禁用 file_get_contents(),因为它非常危险。

    该网站可能有用户代理检测。你可以在你的 curl 调用中伪造它,但我不相信 file_get_contents() 可以做到这一点。网站使用的另一种方法是仅在设置 cookie 后才显示内容,这样网站抓取工具就永远不会看到数据。

    试试这个:

    function curl_scrape($url,$data,$proxy,$proxystatus)
    {
        $fp = fopen("cookie.txt", "w");
        fclose($fp);
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
        if ($proxystatus == 'on')
        {
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
        }
    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        ob_start(); // prevent any output
        return curl_exec ($ch); // execute the curl command
        ob_end_clean(); // stop preventing output
        curl_close ($ch);
        unset($ch);
    }
    

    【讨论】:

    • 已经测试了 cookie 的想法(它不关心 cookie),以及根据其他答案伪造用户代理.. 没有骰子
    【解决方案3】:

    我猜我真的被屏蔽了。现在使用代理,它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-19
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2021-06-18
      • 2015-08-08
      相关资源
      最近更新 更多