在 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
1 2 3 4 5 | 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/qdx/src/api/controllers/UserController.php:189 Stack trace: #0 [internal function]: yii\base\ErrorHandler->handleError() #1 /home/wwwroot/qdx/src/api/controllers/UserController.php(189): file_get_contents() |
2、PHP 需要正确的 CA 证书来验证远程服务器的 SSL 证书。如果 CA 证书缺失或过期,会导致 SSL 验证失败。
1 2 3 4 5 6 7 8 | [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
1 | openssl.cafile = /etc/ssl/certs/cacert.pem |
4、最后不得不基于 curl 重新实现,终于解决。参考:在 curl 请求 https 网址,响应为 false 的排查分析
近期评论