记录 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 协议》,转载必须注明作者和本文链接
悲观者永远正确,乐观者永远前行。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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