【发布时间】:2014-11-07 21:15:22
【问题描述】:
案例:我想打开到localhost 的 SSL 连接,而 SSL 证书是 FQDN 的问题。
问题:如果没有在(*) 行中进行特殊处理,下面的程序将失败并显示以下消息:
PHP Warning: stream_socket_enable_crypto(): Peer certificate CN='myhost.com' did not match expected CN='localhost' in test.php
测试PHP程序:
$fp = stream_socket_client("tcp://localhost:993", $errno, $errstr, 30);
// (*) if commented, the program fails
//stream_context_set_option($fp, 'ssl', 'verify_peer_name', false);
if (!$fp) {
die("Unable to connect: $errstr ($errno)");
}
if (!stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
die("Failed to start SSL");
}
fwrite($fp, "USER god\r\n");
fwrite($fp, "PASS secret\r\n");
while ($motd = fgets($fp)) {
echo $motd;
}
fclose($fp);
由于我有很多遗留代码,我希望通过仅对 php.ini(或 CLI)应用更改来获得解决方案,但不幸的是,以下都不起作用:
php -d verify_peer_name=false test.php
php -d ssl.verify_peer_name=false test.php
想法?
参考资料:
【问题讨论】:
-
这只是一个警告,它不应该失败
-
它向控制台打印警告,但实际上
stream_socket_enable_crypto()返回 0(失败)。 -
您找到解决方案了吗?
-
哪个 PHP 版本?
-
@Thibaut:如果你问我,那么我实际上已经按照以下答案得出了解决方案,即我必须编写一些 PHP 代码(看this post,搜索
function sqenable_ssl($stream))。