PHP无法获取https内容

标签 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" 
    }

谢谢你

最佳答案

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

关于PHP无法获取https内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650679/

相关文章:

apache - Tomcat Assets 无法通过 Apache 反向代理加载

Python 请求 lib 与 SSL 版本 3

php - 重定向到新页面

php - Codeigniter 安全删除数据库条目

php - 在 LAMPP for Ubuntu 中将 PHP 从 5.5 升级到 5.6

ssl - 在 Amazon CloudFront 上通过 HTTPS 提供静态内容

python - VS Code 中的请求出现错误,原因是 SSLError ("Can' t connect to HTTPS URL,因为 SSL 模块不可用。”)

javascript - jQuery DataTables - 单击列时排序不起作用

ssl - IIS 重写反向代理在使用 SSL 时出现 HTTP 502 错误

android - HttpsUrlConnection:在 2.3 上找不到证书路径的信任 anchor