【问题标题】:file_get_contents with Proxy带代理的 file_get_contents
【发布时间】:2019-04-03 10:09:19
【问题描述】:

我在使用带有file_get_contents 的代理路由时遇到了一些问题。当我将它与cURL 一起使用时,它可以工作,但不能与此代码一起使用:

$headers = array('Content-Type: text/xml');
$url = "http://google.com/";
$proxy = 'http://xx.xxx.xxx.xxx:443';
$opts = [
    "http" => [
        'proxy' => $proxy,
        "method" => "POST",
        "header" => implode("\r\n", $headers),
        'timeout' => 500,
        "content" => $request,
    ],
];
$stream_context = stream_context_create($opts);
$data = file_get_contents($url, false, $stream_context);
return $data;

问题是这没有首先访问我的代理服务器,而是直接访问 google.com。


当我将此代码与 cURL 一起使用时,它 完美运行:

$handle = curl_init();
$url = "http://google.com";
$proxy = 'xx.xxx.xxx.xxx:443';
// Set the url
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_PROXY, $proxy);
$output = curl_exec($handle);
curl_close($handle);
echo $output;

谁能看出问题出在哪里?

【问题讨论】:

  • 我能找到的所有proxy 的示例都使用tcp 方案而不是http,即使对于HTTP 代理也是如此。
  • 我用tcphttp都试过了

标签: php proxy file-get-contents


【解决方案1】:

我认为你使用了错误的协议

尝试使用:

$proxy = 'tcp://xx.xxx.xxx.xxx:443';

还可以尝试将以下内容添加到您的 $opts 数组中:

'request_fulluri'=>true

【讨论】:

  • 我尝试使用 `$proxy = 'tcp://xx.xxx.xxx.xxx:443' 并在 opts 中添加了 'request_fulluri=>true' 但仍然无法通过我的代理
  • 谁有解决办法?
猜你喜欢
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
相关资源
最近更新 更多