从URL获取文件内容? [英] Get file content from URL?

查看:137
本文介绍了从URL获取文件内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在浏览器中使用以下URL时,它提示我下载带有JSOn内容的文本文件.

When I use following URL in browser then it prompt me to download a text file with JSOn content.

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

(单击上面的URL查看下载的文件内容)

(Click above URL see downloaded file content)

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

Now I want to create a php page. I want that when I call this php page, it should call above URL and get content(json format) from file and show it on screen.

我该怎么做??

推荐答案

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

Depending on your PHP configuration, this may be a easy as using:

$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读取数据,如下所示:

However, if allow_url_fopen isn't enabled on your system, you could read the data via CURL as follows:

<?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.

Incidentally, if you just want the raw JSON data, then simply remove the json_decode.

这篇关于从URL获取文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆