php file_get_contents 忽略了 verify_peer=>false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/15076819/
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
file_get_contents ignoring verify_peer=>false?
提问by Tiberiu-Ionu? Stan
file_get_contents with https hosts works just fine, except for a particular host (test api server from some company - ip whitelisted, can't give you URL to test). This rules out not loaded https modules and other initial setup mistakes.
带有 https 主机的 file_get_contents 工作正常,但特定主机除外(来自某些公司的测试 api 服务器 - ip 列入白名单,无法为您提供 URL 进行测试)。这排除了未加载的 https 模块和其他初始设置错误。
I have tested with multiple PHP installations, all at v5.3.3, 32bits, Debian 32bits.
我已经对多个 PHP 安装进行了测试,所有版本均为 v5.3.3、32 位、Debian 32 位。
The request works with cURL, but only if setting curl_setopt($curl, 
CURLOPT_SSL_VERIFYPEER, 0);. However, setting verify_peer"=>falseon the context for file_get_contents seems to make no difference.
该请求适用于 cURL,但前提是设置curl_setopt($curl, 
CURLOPT_SSL_VERIFYPEER, 0);. 但是,设置verify_peer"=>falsefile_get_contents 的上下文似乎没有区别。
With file_get_contents, the exact same request (same URL, same XML POST data) fails with SSL: Connection reset by peer:
使用 file_get_contents,完全相同的请求(相同的 URL,相同的 XML POST 数据)因SSL失败:连接重置由对等:
$arrContextOptions=array(
    "http" => array(
        "method" => "POST",
        "header" => 
            "Content-Type: application/xml; charset=utf-8;\r\n".
            "Connection: close\r\n",
        "ignore_errors" => true,
        "timeout" => (float)30.0,
        "content" => $strRequestXML,
    ),
    "ssl"=>array(
        "allow_self_signed"=>true,
        "verify_peer"=>false,
    ),
);
file_get_contents("https://somedomain:2000/abc/", false, stream_context_create($arrContextOptions));
.
.
Has anyone encountered this with file_get_contents? Any ideas how to debug?
有没有人在 file_get_contents 中遇到过这个问题?任何想法如何调试?
采纳答案by Skeets
You missed verify_peer_name. If you set that to false as well, the request works:
你错过了verify_peer_name。如果您也将其设置为 false,则请求有效:
$arrContextOptions=array(
    "http" => array(
        "method" => "POST",
        "header" => 
            "Content-Type: application/xml; charset=utf-8;\r\n".
            "Connection: close\r\n",
        "ignore_errors" => true,
        "timeout" => (float)30.0,
        "content" => $strRequestXML,
    ),
    "ssl"=>array(
        "allow_self_signed"=>true,
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);
file_get_contents("https://somedomain:2000/abc/", false, stream_context_create($arrContextOptions));
回答by Nick Andriopoulos
dont' know if this will actually help, but do try removing the SSLoptions from your option array. 
不知道这是否真的有帮助,但请尝试SSL从您的选项数组中删除选项。
The reason behind this:
according to http://www.php.net/manual/en/context.ssl.php, verify_peeris falseby default.
这背后的原因:根据http://www.php.net/manual/en/context.ssl.php,verify_peer是false默认。
allow_self_signedREQUIRES verify_peer, and is falseby default. 
allow_self_signedREQUIRES verify_peer,false默认情况下是。
From the above, I gather that allow_self_signedprobably overrides your setting for verify_peer. 
从上面可以看出,我认为这allow_self_signed可能会覆盖您对verify_peer.
So please try without any option for SSL, or without the allow_self_signed, and let us know if that helped any.
因此,请尝试不使用SSL或不使用 的任何选项allow_self_signed,并让我们知道这是否有帮助。
回答by M Rostami
try this code :
试试这个代码:
$fp = fsockopen("ssl://somedomain/abc/", 2000 , $ErrNo, $ErrString, 30);
if (!$fp) {
    echo "Error No : $ErrNo - $ErrString <br />\n";
} else {
    $out  = "POST / HTTP/1.1\r\n";
    $out .= "Host: somedomain \r\n";
    $out .= "Content-Type: application/xml; charset=utf-8;\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
if you don't get error , i think problem (with file_get_contents) is form client php configuration otherwise from server configuration.
如果您没有收到错误,我认为问题(file_get_contents)是表单客户端 php 配置,否则来自服务器配置。
回答by eyevan
回答by user2649371
only install this
只安装这个
yum install ca-certificates.noarch

