问题描述
当我在浏览器中使用以下 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
(点击上面的网址查看下载的文件内容)
现在我想创建一个 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 获取文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!