PHP无法获取https内容

时间:2015-07-27 10:25:25

标签: php ssl https file-get-contents

我正在使用php中的api。

这是我到目前为止所得到的:

$keybotlink = file_get_contents('https://steamgaug.es/api/v2');
echo $keybotlink;

(不多:D),无论如何,如果我试图运行它,页面是空的。

如果我尝试

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_dump($w);

这是输出:

openssl: yes 
http wrapper: yes 
https wrapper: yes 
wrappers: array(22) 
      { [0]=> string(13) "compress.zlib" 
        [1]=> string(4) "dict" 
        [2]=> string(3) "ftp" 
        [3]=> string(4) "ftps" 
        [4]=> string(6) "gopher" 
        [5]=> string(4) "http" 
        [6]=> string(5) "https" 
        [7]=> string(4) "imap" 
        [8]=> string(5) "imaps" 
        [9]=> string(4) "pop3" 
        [10]=> string(5) "pop3s" 
        [11]=> string(4) "rtsp" 
        [12]=> string(4) "smtp" 
        [13]=> string(5) "smtps" 
        [14]=> string(6) "telnet" 
        [15]=> string(4) "tftp" 
        [16]=> string(3) "php" 
        [17]=> string(4) "file" 
        [18]=> string(4) "glob" 
        [19]=> string(4) "data" 
        [20]=> string(3) "zip" 
        [21]=> string(4) "phar" 
    }

谢谢,我

2 个答案:

答案 0 :(得分:0)

对于来自HTTPS站点的下载内容,请使用以下代码:

<?php
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://steamgaug.es/api/v2");
curl_setopt($ch, CURLOPT_HEADER, 0);
// if you want to connet to https page you can skip SSL verification by setting CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to 0
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0 );
// if you want to fetch result to a variable use CURLOPT_RETURNTRANSFER
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL and pass it to the $output variable
$output = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

echo $output;

CURL非常好,并且充满了处理HTTP(S)请求的选项工具。

答案 1 :(得分:0)

要允许https包装php_openssl扩展名必须启用且allow_url_fopen必须设置为开启。

如果不存在,请在php.ini文件中添加以下行:

extension=php_openssl.dll

allow_url_fopen = On

您也可以使用curl

function getData($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);

    if($result === false)
    {
        echo 'Curl error: ' . curl_error($ch);
    }
    else
    {
        echo 'Operation completed without any errors';
    }
    curl_close($ch);
    return $result;
}

print_r( json_decode( getData('https://steamgaug.es/api/v2') ) );