我正在尝试在本地主机上使用 fsockopen,在 Windows 上使用 https,使用 Wamp。 它在 http 上运行良好,但在 https 上运行不正常。
我使用 OpenSSL ( How to install: OpenSSL + WAMP ) 创建了一个证书,并在 httpd-vhosts.conf 文件中声明了一个虚拟主机。
这是 PHP 代码:
$fp = fsockopen("ssl://localhost", 443, $errno, $errstr, FSOCKOPEN_TIMEOUT); // same pb with ssl://www.localhost
这会产生以下错误:
PHP Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
PHP Warning: fsockopen(): Failed to enable crypto
PHP Warning: fsockopen(): unable to connect to ssl://localhost:443 (Unknown error)
当 Apache 启动时,我的 ssl 错误日志文件中也有以下警告(我不知道是否相关):
[ssl:warn] [pid 6008:tid 596] AH01909: localhost:443:0 server certificate does NOT include an ID which matches the server name
你知道我做错了什么吗?
谢谢!
最佳答案
请记住 - 如果您自己在本地创建 ssl 证书,它通常不受客户端(例如网络浏览器)的信任
完成证书创建过程后,系统会询问您有关通用名称 (CN) 的信息。那应该是您计划在其上提供网页服务的域,或者,当您仅在本地使用它时,它也可以是本地主机。
在您的情况下,您使用了与您的 apache 配置中的 ServerName
或 ServerAlias
不匹配的不同内容。
Atm 我不明白你为什么要通过 ssl 连接到本地主机——从安全的角度来看,这并不是真正必要的。
否则您可以强制您的客户端 (php) 不检查证书的有效性
<?php
$context = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]);
$fp = stream_socket_client("ssl://localhost", $errno, $errstr, ini_get("default_socket_timeout"), STREAM_CLIENT_CONNECT, $context);
但只对本地连接(!)这样做
关于php - SSL 的 fsockopen 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51010948/