error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in file_get_contents

error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in file_get_contents

当使用 file_get_contents() 访问 HTTPS URL 时,我在日志中得到以下信息:

[21-Jun-2021 10:21:23 Australia/Sydney] PHP Warning:  file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in C:\path\to\script.php on line 174
[21-Jun-2021 10:21:23 Australia/Sydney] PHP Warning:  file_get_contents(): Failed to enable crypto in C:\path\to\script.php on line 174
[21-Jun-2021 10:21:23 Australia/Sydney] PHP Warning:  file_get_contents(https://...): failed to open stream: operation failed in C:\path\to\script.php on line 174

该网站在我的浏览器中加载得很好。我还下载了 cURL 的 CA 证书并告诉 PHP 在哪里可以找到它们。

原来我的 Apache 配置中的证书不完整。我只有证书本身,也没有中间证书。该站点在浏览器中运行良好,但当我尝试在 PHP 中运行时失败了。出于同样的原因,工作中的网络防火墙也拒绝让我通过类似配置的网站。用包含其中介的证书文件替换它解决了这个问题。

要检查您的网站,请转至 https://whatsmychaincert.com 并输入您的网站域。如果您的网站没有中介,它会为您提供包含相关中介的完整证书文件供您安装。

如果有问题的网站是内部网站,您可以将证书粘贴到第二个表单中,它也会这样做:

不需要获取根证书,因为客户端已经有了。