【问题标题】:PHP Can't get https contentPHP 无法获取 https 内容
【发布时间】:2015-10-17 11:54:31
【问题描述】:

我正在努力在 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" 
    }

谢谢,我

【问题讨论】:

    标签: php ssl https file-get-contents


    【解决方案1】:

    要允许https wrapper php_openssl 扩展必须启用并且allow_url_fopen 必须设置为on。

    如果 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') ) );
    

    【讨论】:

    • 我不能做 extension=php_openssl.dll allow_url_fopen = On,它不是我自己的服务器。
    • 如果我尝试 getData,我又会得到一个空白屏幕。
    • 你在做echo getData('https://steamgaug.es/api/v2'); 因为这对我有用。或者更好print_r( json_decode( getData('https://steamgaug.es/api/v2') ) );
    • 已更新我的帖子以检查 curl 错误。执行它并检查你得到了什么错误。
    • Curl 错误:错误:14077438:SSL 例程:SSL23_GET_SERVER_HELLO:tlsv1 警报内部错误
    【解决方案2】:

    要从 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) 请求。

    【讨论】:

    • 如果我将其粘贴到 .php 中,我会收到“警告:curl_setopt() 期望参数 1 是资源,在第 3 行的 /home/tf2bots/public_html/test.php 中给出 null 警告:curl_setopt () 期望参数 1 是资源,在 [ALL LINES] 行的 /home/tf2bots/public_html/test.php 中给出 null
    • 再次检查代码,代码顶部丢失,现在可见
    • 我不知道为什么stackoverflow的前两行被吃掉了:(
    • 它用上面的代码显示了对我的 JSON 响应。你确定,你启用了 curl 吗?另外,请尝试清除缓存。
    • 尝试将代码 ini_set('display_errors', 1);error_reporting(E_ALL); 放在脚本顶部
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多