带代理的file_get_contents

问题描述 投票:1回答:1

我在使用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;

有人能看出问题是什么吗?

php proxy file-get-contents
1个回答
0
投票

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

尝试使用:

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

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

'request_fulluri'=>true
© www.soinside.com 2019 - 2024. All rights reserved.