记录 openssl 证书验证失败的诡异问题

项目使用了第三方包 tecnickcom/TCPDF 生成 PDF 文件,需要插入图片到文件中。
然后发生了以下报错:

[ error ] [2]getimagesize(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
[ error ] [2]getimagesize(): Failed to enable crypto
[ error ] [2]getimagesize(https://www.xxx.com/static/upload/default-avatar.jpg): failed to open stream: operation failed
[ error ] [2]file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed

解决方案:

PHP 在发起 HTTPS 请求的时候,需要本地证书自验,也可以跳过不验证,我这里使用了验证证书。
先下载一个 CA 证书

wget http://curl.haxx.se/ca/cacert.pem

配置一下 php.ini

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
openssl.cafile= SSL证书绝对路径

问题解决。

一般情况是不需要理会这个证书的,就是不明白为什么 cafile 证书突然就失效了,但是 curl 是能发出 https 请求的,所以感到很诡异,换了 cafile 证书之后就正常了。

本作品采用《CC 协议》,转载必须注明作者和本文链接
悲观者永远正确,乐观者永远前行。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!