【问题标题】:Using proxy without cURL使用没有 cURL 的代理
【发布时间】:2016-06-10 19:12:52
【问题描述】:

我尝试使用这两种方法在 php 中使用代理。我有 socks5 代理。当我使用 cURL 方法时效果很好。但是当我尝试使用stream_context_set_default 时它根本不起作用。有没有办法使用代理?例如:此代理 207.174.151.226:45021 不适用于 stream_context_set_default,但适用于 cURL。我不想使用 cURL 使用代理,因为我的 php 文件中到处都需要代理。对此有任何帮助吗?代码:

<?php
$default_opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar",
    'proxy'=>"186.34.59.99:10000"
  )
);

$default = stream_context_set_default($default_opts);

$homepage = file_get_contents('http://www.google.com');
echo $homepage;


?>

这行得通:

<?php
$url = "http://www.google.co.uk";
$ch = curl_init();

if($ch === false)
{
    die('Failed to create curl object');
}

$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

//proxy details
curl_setopt($ch, CURLOPT_PROXY, '186.34.59.99:10000');
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

$data = curl_exec($ch);
curl_close($ch);

echo $data;

?>

谢谢!

【问题讨论】:

  • 发布您的实际代码,包括您的 stream_context_create 调用
  • @Sjon 完成!添加了我的代码。
  • 您是否尝试按照手册中的说明在您的代理地址前加上 tcp://
  • 是的。然后显示警告:file_get_contents(google.co.uk): failed to open stream: HTTP request failed!在第 13 行的 proxy.php 中

标签: php curl proxy socks stream-socket-client


【解决方案1】:

确保您发送的 URI 包含原始主机名。您可以通过显式传递request_fulluri = true 作为$default_opts['http'] 的一部分来实现这一点,如http://php.net/manual/en/context.http.php 所述

【讨论】:

  • 还是一样的结果。我已经添加了这个参数。
猜你喜欢
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多