PHP 无法获取 https 内容

PHP Can't get https content

我正在研究如何在 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" 
    }

谢谢,我

要从 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 非常好并且充满了处理 HTTPS) 请求的选项工具。

要允许 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') ) );