php解决file_get_contents无法获取https页面的内容 - 回这世界

今天在使用file_get_contents函数获取远程页面内容的时候出现了一点问题,显示Unable to find the wrapper "https",这里请求页面地址是使用https协议,现在跟大家分享一下如何解决file_get_contents不支持https网址。

有三种解决办法:


第一:修改php.ini配置文件

windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。(注意allow_url_fopen也必须开启)


linux下的PHP,就必须安装openssl模块,安装好了以后就可以访问了。


第二:stream_context_create方法

以下代码允许你使用file_get_contents获取https页面内容:

$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));

第三:使用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;
}

点赞(0) 打赏

书签

技术

知识

立即下载

视频

导图

配色

教程

代码

素材

评论列表 0

暂无评论

发表评论 取消回复

分享
链接
快捷
记账
快捷
运动
热门
书签
菜单
显示
返回
顶部