PHP file_get_contents(): SSL operation failed with code 1. OpenSSL Error message...
在调试php脚本代码时,发现使用 file_get_contents() 函数请求HTTPS的网址链接时出现了报错,其报错代码如下面所示“file_get_contents(): SSL operation failed with code 1. OpenSSL Error message...”百度查了一下原因与解决方法,下面就来分享一下。
原因:
服务器上未能正确配置好https证书,所以出现了错误。
解决方法:
方法1:
(1)、下载证书,http://curl.haxx.se/ca/cacert.pem
(2)、证书上传到服务器,并记录好上传的路径
(3)、打开php的配置文件,php.ini
(4)、修改 openssl.cafile 的配置的值为上传的证书地址即可
例如:
1 openssl.cafile = "/etc/ssl/certs/cacert.pem"
(5)、重启 php 即可
方法2:
直接对 file_get_contents 函数进行设置,让 file_get_contents 函数跳过https验证,暂时解决问题
例:
1 # feiniaomy.com 飞鸟慕鱼博客 2 $url = '请求地址'; 3 $arrContextOptions=array( 4 "ssl"=>array( 5 "verify_peer"=>false, 6 "verify_peer_name"=>false, 7 "allow_self_signed"=>true, 8 ), 9 ); 10 $response = file_get_contents($url, false, stream_context_create($arrContextOptions));
方法3:
使用curl来替代 file_get_contents 函数来使用!
例:
1 # feiniaomy.com 飞鸟慕鱼博客 2 $url = '请求地址'; 3 function getSSLPage($url) { 4 $ch = curl_init(); 5 curl_setopt($ch, CURLOPT_HEADER, false); 6 curl_setopt($ch, CURLOPT_URL, $url); 7 curl_setopt($ch, CURLOPT_SSLVERSION,3); 8 $result = curl_exec($ch); 9 curl_close($ch); 10 return $result; 11 } 12 var_dump(getSSLPage($url));
来源:https://www.feiniaomy.com/post/1021.html
本文来自博客园,作者:郭德纲又打人了嘿,转载请注明原文链接:https://www.cnblogs.com/blibli/p/17805825.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗