如何使用TLS

问题描述:

在PHP7.0时做出的fsockopen忽略证书,使用fsockopen连接打开HTTPS连接时(在同一 主机托管),使用主机名状tls://foo.localhost,我得到一个错误, 看起来像如何使用TLS

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

连接未打开。

相同的代码在PHP 5.5中工作(这是不太严格的检查点)。我做的 并不真正关心验证证书(这个代码在单元测试中运行,而 集成套件只连接到本地主机)。尽管如此,测试需要依照 http和https运行。

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

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

+0

是否'verify_peer' [上下文选项]的

实施例(http://php.net/manual/en/context.ssl.php)影响本? –

+0

@ÁlvaroGonzález,我不知道如何传递一个上下文到'fsockopen' – juanleon

+0

从来没有与套接字工作,我认为[stream_context_set_default()](http://php.net/)应该工作。如果没有,请尝试在[fsockopen()](http://php.net/fsockopen)手册页中建议的函数:“函数[stream_socket_client()](http://php.net/manual/en /function.stream-socket-client.php)是相似的,但提供了更丰富的选项集,包括非阻塞连接和提供流上下文的能力。“ –

fsockopen不提供忽略对等证书错误的方法。您必须使用stream_socket_clientcontext。可用的ssl/tls上下文选项可用here。打开插座没有同行验证

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

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