15

带有 https 主机的 file_get_contents 工作得很好,除了特定的主机(来自某些公司的测试 api 服务器 - ip 白名单,不能给你测试的 URL)。这排除了未加载的 https 模块和其他初始设置错误。

我已经测试了多个 PHP 安装,都在 v5.3.3、32 位、Debian 32 位。

该请求适用于 cURL,但前提是设置curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);. 但是,设置verify_peer"=>falsefile_get_contents 的上下文似乎没有什么区别。

使用 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));

.

有人用 file_get_contents 遇到过这个问题吗?任何想法如何调试?

4

5 回答 5

5

你错过了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));
于 2018-08-16T23:17:16.927 回答
1

不知道这是否真的有帮助,但请尝试SSL从您的选项数组中删除选项。

这背后的原因:根据http://www.php.net/manual/en/context.ssl.php,默认情况下verify_peerfalse

allow_self_signed需要verify_peerfalse默认情况下是。

从上面,我收集到allow_self_signed可能会覆盖您对verify_peer.

因此,请尝试不带任何选项SSL或不带allow_self_signed, 并让我们知道这是否有帮助。

于 2013-03-07T06:55:41.847 回答
0

您可以尝试使用Wireshark进行调试——您可能会更好地了解出了什么问题,您应该看到发生了哪个 SSL 错误。

于 2013-03-03T01:25:56.540 回答
0

试试这个代码:

$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);
}

如果您没有收到错误,我认为问题(使用 file_get_contents)来自客户端 php 配置,否则来自服务器配置。

于 2013-03-07T09:00:08.367 回答
-1

只安装这个

yum install ca-certificates.noarch
于 2016-07-31T13:14:33.333 回答