PHP - 通过http代理的https流 [英] PHP - https stream through http proxy
问题描述
我试图通过HTTPS获取流的内容,但我必须经过HTTP代理。
我不想使用cURL,而是使用 fopen 和上下文参数。
I'm trying to get the content of a stream over HTTPS, but I have to go over an HTTP proxy. I'd like not to use cURL but rather use fopen with a context argument.
这不是工作:
$stream = stream_context_create(Array("http" => Array("method" => "GET",
"timeout" => 20,
"proxy" => "tcp://my-proxy:3128",
'request_fulluri' => True
)));
echo file_get_contents('https://my-stream', false, $context);
此 DOES 工作(cURL):
$url = 'https://my-stream';
$proxy = 'my-proxy:3128';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
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;
有人知道第一段代码有什么问题吗?如果它与cURL一起工作,则必须有一种方法使其与上下文一起工作。
我试图将上下文选项改为一堆不同的值,没有运气。
Does somebody know what is wrong with the first piece of code ? if it works with cURL there has to be a way to make it work with a context. I tried to change the context options to a bunch of different values woth no luck.
任何帮助将非常感谢!
感谢。
推荐答案
您没有指定确切的错误讯息,请尝试添加 ignore_errors => true
。但是,如果你从Apache得到一个 400 Bad Request
,你可能会碰到的问题是一个服务器名称指示&主机头不匹配。还有一个与此相关的PHP错误: https://bugs.php.net/bug.php?id= 63519
You did not specifiy the exact error message, try adding ignore_errors => true
. But if you are getting a 400 Bad Request
from Apache, the problem you are probably hitting, is a Server Name Indication & host header mismatch. There is also a PHP bug related to this: https://bugs.php.net/bug.php?id=63519
尝试以下修正,直到该错误得到解决:
Try the following fix until this bug is resolved:
$stream = stream_context_create(array(
'http' => array(
'timeout' => 20,
'proxy' => 'tcp://my-proxy:3128',
'request_fulluri' => true
),
'ssl' => array(
'SNI_enabled' => false // Disable SNI for https over http proxies
)
));
echo file_get_contents('https://my-stream', false, $context);
这篇关于PHP - 通过http代理的https流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!