php解决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.web3.xin/ */
    $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;
}
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2024

备案号:京ICP备15003423号-3