如何使用tls

时间:2017-01-30 11:04:45

标签: php ssl

在PHP7.0中,使用fsockopen连接时打开https连接(托管在同一个连接中) 主机),使用像tls://foo.localhost这样的主机名,我收到一个错误 看起来像

fsockopen(): Peer certificate CN="bar" did not match expected CN="foo.localhost"

未打开连接。

相同的代码在PHP 5.5中工作(不太严格的检查点)。我做 并不真正关心验证证书(此代码在单元测试中运行 集成套件,仅连接到localhost)。测试需要运行 但是http和https。

我知道在使用file_get_contents时如何禁用这些检查。

如何在使用fsockopen时禁用对等证书验证?

1 个答案:

答案 0 :(得分:6)

fsockopen没有提供忽略对等证书错误的方法。

您必须将stream_socket_clientcontext一起使用。可用的ssl / tls上下文选项可用here

在没有对等验证的情况下打开套接字的示例

$context = stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ]
]);

$hostname = "tls://foo.localhost";
$socket = stream_socket_client($hostname, $errno, $errstr, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);