php 如何让 file_get_contents() 使用 HTTPS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1975461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get file_get_contents() to work with HTTPS?
提问by James Simpson
I'm working on setting up credit card processing and needed to use a workaround for CURL. The following code worked fine when I was using the test server (which wasn't calling an SSL URL), but now when I am testing it on the working server with HTTPS, it's failing with the error message "failed to open stream".
我正在设置信用卡处理,需要使用 CURL 的解决方法。当我使用测试服务器(它没有调用 SSL URL)时,以下代码运行良好,但是现在当我使用 HTTPS 在工作服务器上测试它时,它失败并显示错误消息“无法打开流”。
function send($packet, $url) {
$ctx = stream_context_create(
array(
'http'=>array(
'header'=>"Content-type: application/x-www-form-urlencoded",
'method'=>'POST',
'content'=>$packet
)
)
);
return file_get_contents($url, 0, $ctx);
}
采纳答案by VolkerK
Try the following script to see if there is an https wrapper available for your php scripts.
尝试以下脚本以查看是否有可用于您的 php 脚本的 https 包装器。
$w = stream_get_wrappers();
echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
the output should be something like
输出应该是这样的
openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(11) {
[...]
}
回答by hassan
To allow https wrapper:
允许 https 包装器:
- the
php_opensslextension must exist and be enabled allow_url_fopenmust be set toon
- 在
php_openssl扩展必须存在且已启用 allow_url_fopen必须设置为on
In the php.ini file you should add this lines if not exists:
在 php.ini 文件中,如果不存在,您应该添加以下行:
extension=php_openssl.dll
allow_url_fopen = On
回答by Benjamin Crouzier
Try the following.
请尝试以下操作。
function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Note: This disables SSL verification, meaning the security offered by HTTPS is lost. Only use this code for testing / local development, never on the internetor other public-facing networks. If this code works, it means the SSL certificate isn't trusted or can't be verified, which you should look into fixing as a separate issue.
注意:这会禁用 SSL 验证,这意味着HTTPS 提供的安全性将丢失。仅将此代码用于测试/本地开发,切勿在 Internet或其他面向公众的网络上使用。如果此代码有效,则意味着 SSL 证书不受信任或无法验证,您应该将其作为单独的问题进行修复。
回答by Dushyant Dagar
$url= 'https://example.com';
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));
This will allow you to get the content from the url whether it is a HTTPS
这将允许您从 url 获取内容,无论它是 HTTPS
回答by Emil Vikstr?m
This is probably due to your target server not having a valid SSL certificate.
这可能是由于您的目标服务器没有有效的 SSL 证书。
回答by Ritesh Chandora
Just add two lines in your php.ini file.
只需在 php.ini 文件中添加两行。
extension=php_openssl.dll
extension=php_openssl.dll
allow_url_include = On
allow_url_include = On
its working for me.
它为我工作。
回答by Jim Jones
I had the same error. Setting allow_url_include = Onin php.inifixed it for me.
我有同样的错误。设置allow_url_include = On在php.ini固定对我来说。
回答by Gordon
HTTPS is supported starting from PHP 4.3.0, if you have compiled in support for OpenSSL.
Also, make sure the target server has a valid certificate, the firewall allows outbound connections and allow_url_fopenin php.ini is set to true.
如果您编译支持 OpenSSL,则从 PHP 4.3.0 开始支持 HTTPS。另外,请确保目标服务器具有有效的证书,防火墙允许出站连接并且allow_url_fopen在 php.ini 中设置为 true。
回答by dtbarne
In my case, the issue was due to WAMP using a different php.ini for CLI than Apache, so your settings made through the WAMP menu don't apply to CLI. Just modify the CLI php.ini and it works.
就我而言,问题是由于 WAMP 对 CLI 使用的 php.ini 与 Apache 不同,因此您通过 WAMP 菜单进行的设置不适用于 CLI。只需修改 CLI php.ini 即可。
回答by GZipp
Sometimes a server will choose not to respond based on what it sees or doesn't see in the http request headers (such as an appropriate user agent). If you can connect with a browser, grab the headers it sends and mimic them in your stream context.
有时,服务器会根据它在 http 请求标头中看到或没有看到的内容(例如适当的用户代理)选择不响应。如果您可以连接浏览器,请获取它发送的标头并在您的流上下文中模仿它们。

