【问题标题】:curl through proxy returns no content通过代理卷曲不返回任何内容
【发布时间】:2011-06-15 17:26:09
【问题描述】:

我现在正在编写一个 PHP 脚本,该脚本向我们学校的服务器发送请求,以获取有关不同课程班级规模的实时信息。当我不使用代理时,脚本运行得非常好,返回一个包含课程编号和可用座位的字符串。但是,我想为学生提供这项服务,我担心如果我提出太多请求,我的 ip 会被阻止。所以我试图通过代理来做到这一点,但没有成功。只要我将 CURLOPT_HTTPPROXYTUNNEL 和 CURLOPT_PROXY 字段添加到我的请求中,就不会返回任何内容。我什至不确定此时如何对其进行故障排除,因为我没有收到任何类型的错误消息。有谁知道发生了什么或如何解决它?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$proxy = explode(':', $proxy);
curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'tempcookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'tempcookie.txt');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref);
$exec = curl_exec($ch);

echo curl_error($ch);
print_r(curl_getinfo($ch));
echo $exec;

用于测试的代理:75.147.173.215:8080

【问题讨论】:

  • 为什么不缓存结果而不是作弊?

标签: php curl


【解决方案1】:

如果我必须使用带有 curl 的代理,我会使用以下代码:

$proxy = "127.0.0.1:8080"; // or something like that

if($proxy !== null){

    // no need to specify PROXYPORT again
    curl_setopt($ch, CURLOPT_PROXY, $proxy);

    // to make the request go through as though proxy didn't exist
    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);

}

【讨论】:

    【解决方案2】:

    您可以设置 CURLOPT_STDERR 和 CURLOPT_VERBOSE curl options 以将错误保存在文件中。此外,您可以使用curl_error() 函数。顺便说一句,默认情况下,curl 应该在 STDERR 中显示所有错误。

    此外,为了进行一般检查,您可以简单地在浏览器配置属性中指定选择的代理,尝试在浏览器中打开特定服务,看看是否返回正确的响应。

    更新:

    CURLOPT_HTTPPROXYTUNNEL 用于在请求代理服务器时使 curl 调用 CONNECT HTTP 方法(详见here)。我在没有这个选项的情况下测试了代码——它成功了。

    我使用的代码:

    $proxy = "75.147.173.215:8080";
    $proxy = explode(':', $proxy);
    $url = "http://google.com";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    
    $exec = curl_exec($ch);
    
    echo curl_error($ch);
    print_r(curl_getinfo($ch));
    echo $exec;
    

    【讨论】:

    • 我有一个大约 10k 代理的列表...以这种方式检查它是不真实的
    • 只需从其中一个开始,确保它适用于浏览器,然后尝试使其在您的脚本中运行。之后尝试使用所有 10k 代理。
    • 另外,一些网站会阻止来自一些公共代理的访问,因为它们经常被用于攻击。
    • 它在浏览器中工作,但我收到此错误:使用 curl 时无法连接到主机
    • 您指定什么作为 CURLOPT_PROXY 选项的参数?一般来说,在最初的问题中发布这样的代码很好,因为它通常可以澄清很多问题。
    【解决方案3】:

    这是一个经过良好测试的函数,我将其用于我的项目,并带有详细的自我解释性 cmets


    80 以外的端口经常被服务器防火墙阻止,因此代码在 localhost 上似乎可以正常工作,但在服务器上却没有,请尝试使用端口 80 代理

    function get_page($url){
    
    global $proxy;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    //curl_setopt($ch, CURLOPT_PROXY, $proxy);
    curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return page 1:yes
    curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request timeout 20 seconds
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the url changes
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection responce
    curl_setopt($ch, CURLOPT_USERAGENT,
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt"); // cookies storage / here the changes have been made
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // the page encoding
    
    $data = curl_exec($ch); // execute the http request
    curl_close($ch); // close the connection
    return $data;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 2011-04-26
      • 2010-11-20
      相关资源
      最近更新 更多