PHP升级到7以后直接用file_get_contents和get_headers读取https开头的URL会报错:
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed Warning: get_headers(): Failed to enable crypto Warning: get_headers(): failed to open stream: operation failed
可以用stream_context_create设置不验证的选项,下面是我写的几个函数:
/**
* 函数:读取使用SSL证书的网址
* 输入:$url网址
* 输出:读取网址获得的内容
*/
function file_get_contents_ssl($url) {
$stream_opts = [
"ssl" => [
"verify_peer"=>false,
"verify_peer_name"=>false,
]
];
$contents = file_get_contents($url, false, stream_context_create($stream_opts));
return $contents;
}
/**
* 函数:读取使用SSL证书的网址headers
* 输入:$url网址
* 输出:读取网址获得的数组
*/
function get_headers_ssl($url) {
$stream_opts = [
'ssl' => [
'verify_host' => false,
'verify_peer' => false,
'verify_peer_name' => false,
],
];
$headers = get_headers($url, 1, stream_context_create($stream_opts));
return $headers;
}
/**
* 函数:网址页面是否存在
* 输入:$url网址
* 返回:是或者否
*/
function url_exists($url) {
$array = get_headers_ssl($url);
if ($array == FALSE) return FALSE;
if (strpos(json_encode($array),' 200 OK') != FALSE) {
return TRUE;
} else {
return FALSE;
}
}
这几个函数就可以在其它地方被调用了。
评论2
没看懂,高手就是高www.benedetti
没看懂,高手就是高,能 在详细具体点吗?真有疑问可以留言,但带网址来做宣传就没有必要留言了,谢谢。
真有疑问可以留言,但带网址来做宣传就没有必要留言了,谢谢。