没有不值得去解决的问题,也没有不值得去学习的技术!

在 Yii2 中,file_get_contents(“https://api.weixin.qq.com”); 时报错:yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:

在 Yii2 中,file_get_contents("https://api.weixin.qq.com"); 时报错:yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:

作者:

在

, ,

1、在 Yii2 中,file_get_contents(“https://api.weixin.qq.com”); 时报错:yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:。如图1

在 Yii2 中,file_get_contents("https://api.weixin.qq.com"); 时报错:yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
图1
Plaintext

2025-03-11 17:56:30 [101.204.100.38][1826027541133144][-][error][yii\base\ErrorException:2] yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:0A000126:SSL routines::unexpected eof while reading in /home/wwwroot/object/src/api/controllers/UserController.php:189
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError()
#1 /home/wwwroot/object/src/api/controllers/UserController.php(189): file_get_contents()


2、PHP 需要正确的 CA 证书来验证远程服务器的 SSL 证书。如果 CA 证书缺失或过期,会导致 SSL 验证失败。

Plaintext

[root@iZ2zeaj7tnbv8d3gsoy1w5Z ~]# cp -f cacert.pem /etc/ssl/certs/cacert.pem
[root@iZ2zeaj7tnbv8d3gsoy1w5Z ~]# cd /etc/ssl/certs/
[root@iZ2zeaj7tnbv8d3gsoy1w5Z certs]# ls -l
total 228
lrwxrwxrwx 1 root root     49 Aug 19  2024 ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
lrwxrwxrwx 1 root root     55 Aug 19  2024 ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
-rw-r--r-- 1 root root 233263 Mar 11 17:53 cacert.pem
[root@iZ2zeaj7tnbv8d3gsoy1w5Z certs]#


3、在 php.ini 中配置 openssl.cafile。重启 php-fpm 后,仍然不行 。如图2

在 php.ini 中配置 openssl.cafile。重启 php-fpm 后,仍然不行
图2
Plaintext

openssl.cafile = /etc/ssl/certs/cacert.pem

4、最后不得不基于 curl 重新实现,终于解决。参考:在 curl 请求 https 网址,响应为 false 的排查分析

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系