【问题标题】:Get file content from URL?从 URL 获取文件内容?
【发布时间】:2011-07-28 05:10:51
【问题描述】:

当我在浏览器中使用以下 URL 时,它会提示我下载包含 JSOn 内容的文本文件。

https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json

(点击以上网址查看下载的文件内容)

现在我想创建一个 php 页面。我希望当我调用这个 php 页面时,它应该调用上面的 URL 并从文件中获取内容(json 格式)并将其显示在屏幕上。

我该怎么做??

【问题讨论】:

    标签: php url file-get-contents


    【解决方案1】:

    file_get_contentsjson_decodeecho 结合使用。

    【讨论】:

    • 我正在使用 file_get_contents 来获取内容,但是当我回显它时它不是 JSON。它显示了一些特殊字符。
    • @Awan 你能解决这个问题吗?我还看到了特殊字符。
    【解决方案2】:

    根据您的 PHP 配置,这可能很容易使用:

    $jsonData = json_decode(file_get_contents('https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json'));
    

    但是,如果您的系统上没有启用allow_url_fopen,您可以通过 CURL 读取数据,如下所示:

    <?php
        $curlSession = curl_init();
        curl_setopt($curlSession, CURLOPT_URL, 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json');
        curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
    
        $jsonData = json_decode(curl_exec($curlSession));
        curl_close($curlSession);
    ?>
    

    顺便说一句,如果您只想要原始 JSON 数据,则只需删除 json_decode

    【讨论】:

    • 谢谢你投了赞成票 :) 编辑:我尝试不使用 json_decode 并得到一个完全呈现的 url,但使用解码,什么都没有。解码的目的是什么?
    • @Steven 它将 JSON 字符串解码为 PHP 变量。
    • @John_Parker 非常感谢。我目前正在使用 Google 来呈现 url 的快照,但这并不理想,因为图像很小,但使用 JSON 解码作为 PHP 变量,这是一个我可以拆分并抓取页面标题和第一张图像之类的数组吗?干杯。
    • 从 PHP 5.1.3 开始,BINARYTRANSFER 选项无效:使用 CURLOPT_RETURNTRANSFER 时将始终返回原始输出。 php.net/manual/en/function.curl-setopt.php
    【解决方案3】:
    $url = "https://chart.googleapis....";
    $json = file_get_contents($url);
    

    现在你可以回显 $json 变量,如果你只想显示输出,或者你可以解码它,然后用它做一些事情,像这样:

    $data = json_decode($json);
    var_dump($data);
    

    【讨论】:

      【解决方案4】:

      不要忘记:要获取 HTTPS 内容,应在 php.ini 中启用 OPENSSL 扩展。 (how to get contents of site use HTTPS)

      【讨论】:

        【解决方案5】:

        1) 局部最简单的方法

        <?php
        echo readfile("http://example.com/");   //needs "Allow_url_include" enabled
        //OR
        echo include("http://example.com/");    //needs "Allow_url_include" enabled
        //OR
        echo file_get_contents("http://example.com/");
        //OR
        echo stream_get_contents(fopen('http://example.com/', "rb")); //you may use "r" instead of "rb"  //needs "Allow_url_fopen" enabled
        ?> 
        

        2) 更好的方法是 CURL

        echo get_remote_data('http://example.com'); // GET request 
        echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); // POST request
        

        它会自动处理FOLLOWLOCATION问题+远程网址:
        src="./imageblabla.png"变成:
        src="http://example.com/path/imageblabla.png"

        代码:https://github.com/tazotodua/useful-php-scripts/blob/master/get-remote-url-content-data.php

        【讨论】:

        • 对不起,我如何用 REPLACESSOURCES = True 来称呼它?
        猜你喜欢
        • 2012-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-21
        • 2011-05-12
        相关资源
        最近更新 更多