一、说明
- file_get_contents()读取 https 内容时报错
二、原因
- 本地服务没有配置ssl证书,不能获取到https的路径的内容
三、解决
1、Linux服务器配置https ssl证书;
2、curl_ 请求获取内容(见下面的方法curlGet())
function curlGet($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$content = curl_exec($ch);
curl_close($ch);
return ($content);
}
3、使用file_get_contents()函数跳过https验证
$streamOpts = [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
];
$html = file_get_contents($pageUrl, false, stream_context_create($streamOpts));