当前位置: 主机百科 » 资源 » 技术 » 正文

file_get_contents函数获取https内容出错的解决方法

在php中使用file_get_contents函数抓取https网站内容时出错,可采用以下两种方法解决问题:

方法一:开启openssl扩展

打开php.ini文件找到 ;extension=php_openssl.dll,去掉前面的分号,保存后重启web服务器即可。

方法二:使用curl函数替代file_get_contents

curl抓取https网址内容示例:

1
  <?php  function getHttpsContent($url){  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL,$url);  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  $result = curl_exec($ch);  curl_close ($ch);  return $result;  ?>

未经允许不得转载:主机百科 » file_get_contents函数获取https内容出错的解决方法

相关文章