【发布时间】: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 代理也是如此。 -
我用
tcp和http都试过了
标签: php proxy file-get-contents