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

SSL Operation Failed With Code 1 When Calling file_get_contents in Yii2

在 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. In Yii2, when calling file_get_contents(“https://api.weixin.qq.com”);, an error occurs: yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:. As shown in Figure 1

In Yii2, when calling file_get_contents("https://api.weixin.qq.com");, an error occurs: yii\base\ErrorException: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
Figure 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 requires a valid CA certificate to verify the remote server’s SSL certificate. If the CA certificate is missing or expired, SSL verification will fail.

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. Configure openssl.cafile in php.ini. After restarting php-fpm, it still does not work. As shown in Figure 2

Configure openssl.cafile in php.ini. After restarting php-fpm, it still does not work
Figure 2
Plaintext

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

4. Finally, I had to reimplement the request using curl, which successfully resolved the issue. Reference: Troubleshooting analysis for curl requests to https URLs returning false

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

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

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

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

关于我  ·  GitHub  ·  邮件联系