【问题标题】:How to use CURL via a proxy?如何通过代理使用 CURL?
【发布时间】:2011-07-09 20:57:12
【问题描述】:

我希望将 curl 设置为使用代理服务器。 url 由 html 表单提供,这不是问题。没有代理它工作正常。我在这个网站和其他网站上找到了代码,但它们不起作用。任何帮助找到正确的解决方案将不胜感激。我觉得波纹管很接近,但我错过了一些东西。谢谢。

我改编自这里 http://www.webmasterworld.com/forum88/10572.htm 的波纹管代码,但它返回了一条关于第 12 行缺少 T_VARIABLE 的错误消息。

<?

$url = '$_POST[1]';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1)
curl_exec ($ch); 
$curl_info = curl_getinfo($ch);
curl_close($ch);
echo '<br />';
print_r($curl_info);
?>

以下来自curl through proxy returns no content

<?

$proxy = "66.96.200.39:80";
$proxy = explode(':', $proxy);
$url = "$_POST[1]";

$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;
?>

目前在 pelican-cement.com 上直播,但也无法使用。

更新: 感谢您的所有帮助,我进行了上述更改。现在它只返回一个空白屏幕。

<?

$url = $_POST['1'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_setopt($ch, CURLOPT_PROXY, '66.96.200.39:80');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_exec ($ch); 
$curl_scraped_page = curl_exec($ch);
curl_close($ch);

echo $curl_scraped_page;
?> 

【问题讨论】:

  • 第 12 行缺少分号
  • 另外,您需要将 $url = '$_POST[1]' 更改为 $url = $_POST[1] - 否则,$url 将是一个字符串,而不是您想要的 URL跨度>
  • 另外,$_POST 数组中的键是一个字符串而不是整数,所以你希望它写成$_POST['1']
  • pelican-cement.com 上的表单有名为“firstname”和“lastname”的输入,但没有一个名为“1”的输入。
  • @user586011:请将您的解决方案添加为下面的答案并接受它。不要把解决方案放在问题中,那样效果不好。

标签: php curl proxy php-curl


【解决方案1】:
root@APPLICATIOSERVER:/var/www/html# php connectiontest.php 61e23468-949e-4103-8e08-9db09249e8s1 OpenSSL SSL_connect: SSL_ERROR_SYSCALL 连接到 10.172.123.1:80 root@APPLICATIOSERVER:/var/www/html#

已修复在 php 脚本文件中声明代理设置的帖子问题。

$proxy = '10.172.123.1:80'; curl_setopt($cSession, CURLOPT_PROXY, $proxy); // 带有端口的代理详细信息

【讨论】:

    【解决方案2】:

    我已经解释了 CURL PROXY 所需的各种 CURL 选项的使用。

    $url = 'http://dynupdate.no-ip.com/ip.php';
    $proxy = '127.0.0.1:8888';
    $proxyauth = 'user:password';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);         // URL for CURL call
    curl_setopt($ch, CURLOPT_PROXY, $proxy);     // PROXY details with port
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);   // Use if proxy have username and password
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); // If expected to call with specific PROXY type
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // If url has redirects then go to the final redirected URL.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);  // Do not outputting it out directly on screen.
    curl_setopt($ch, CURLOPT_HEADER, 1);   // If you want Header information of response else make 0
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch);
    
    echo $curl_scraped_page;
    

    【讨论】:

    • 这些 cmets 很有帮助,但其他人应注意,其他选项实际上并不是必需的
    【解决方案3】:

    这是一个删除了你的错误的工作版本。

    $url = 'http://dynupdate.no-ip.com/ip.php';
    $proxy = '127.0.0.1:8888';
    //$proxyauth = 'user:password';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_PROXY, $proxy);
    //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $curl_scraped_page = curl_exec($ch);
    curl_close($ch);
    
    echo $curl_scraped_page;
    

    我已添加CURLOPT_PROXYUSERPWD,以防您的任何代理需要用户名和密码。 我将CURLOPT_RETURNTRANSFER设置为1,这样数据就会返回到$curl_scraped_page变量中。

    我删除了第二个额外的curl_exec($ch);,它将停止返回变量。 我将您的代理 IP 和端口合并为一个设置。

    我还删除了CURLOPT_HTTPPROXYTUNNELCURLOPT_CUSTOMREQUEST,因为它是默认设置。

    如果您不希望返回标头,请注释掉 CURLOPT_HEADER

    要禁用代理,只需将其设置为 null。

    curl_setopt($ch, CURLOPT_PROXY, null);
    

    如有任何问题,请随时提出,我每天都与cURL 合作。

    【讨论】:

    • 很高兴知道您每天都在使用 CURL。我尝试设置一个 socks 代理,它可以在我的本地机器上运行,但不能在我的 linux 专用服务器上运行。有什么想法吗?
    • @coding_idiot 大多数网络主机出于安全原因会阻止不是 80 或 443 的端口。
    • 我已经解决了。我相信其他人会从中受益。
    • @GravyCode:如果我们在这种情况下从某些服务获取代理,我需要传递用户名/密码吗?
    • 我应该如何知道代理端口是否被虚拟主机阻止?
    【解决方案4】:

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


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

    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;
    }
    

    【讨论】:

    • 这对我有帮助: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https 为假
    • @villamejia 但请注意,使用 CURLOPT_SSL_VERIFYPEER = false。这意味着 cURL 在连接到 https 服务器时不会进行任何证书检查,从而使连接容易受到可能的中间人攻击——因此不再保证数据安全。最好使用 CURLOPT_CAPATH 提供一个包含一组有效根证书颁发机构的目录(例如,Debian/Ubuntu 上的/etc/ssl/certs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 2014-03-12
    • 2011-11-20
    • 2019-01-05
    • 2013-07-23
    • 2011-04-09
    相关资源
    最近更新 更多