file_get_contents、fopen请求https地址时返回false错误内容:SSL routines:ssl3_get_server_certificate:certificate verify failed
最近发现项目中的file_get_contents,fopen突然不能用了?
经过排查发现,使用http访问就可以访问成功,带上s就报错返回false;
起初以为是ssl证书问题,换证书再次操作;返回false,详细错误日志:SSL routines:ssl3_get_server_certificate:certificate verify failed;
还是不行;
file_get_contents通过以下三种方式解决
1.修改php.ini配置文件
windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。(注意allow_url_fopen也必须开启)
linux下的PHP,就必须安装openssl模块,安装好了以后就可以访问了。
2.stream_context_create方法
$url= 'https://example.com';
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));
3.使用curl函数替换file_get_contents
function getSslPage($url) {
/* http://www.manongjc.com/article/1428.html */
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
使用方法2解决file_get_contents问题暂时解决,但总感觉哪不对,因为之前是可以访问的;
后来又遇到fopen报错false;
继续查找资料。。。。。。
这是因为PHP无法验证证书,看看phpinfo并查找openssl.cafile,可以从php.ini文件中找;

证书存在,应该是过期了,替换证书。
可以通过以下地址下载最新的ca证书
https://curl.haxx.se/ca/cacert.pem
替换证书,重载php配置;完美解决!!!

浙公网安备 33010602011771号