php 从 URL 获取文件内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5522636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get file content from URL?
提问by Awan
When I use following URL in browser then it prompt me to download a text file with JSOn content.
当我在浏览器中使用以下 URL 时,它会提示我下载包含 JSOn 内容的文本文件。
https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json
https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World&chof=json
(Click above URL see downloaded file content)
(点击上面的网址查看下载的文件内容)
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 页面。我希望当我调用这个 php 页面时,它应该调用上面的 URL 并从文件中获取内容(json 格式)并将其显示在屏幕上。
How can I do this ??
我怎样才能做到这一点 ??
回答by John Parker
Depending on your PHP configuration, this maybe a easy as using:
根据您的 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'));
However, if allow_url_fopen
isn't enabled on your system, you could read the data via CURL as follows:
但是,如果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);
?>
Incidentally, if you just want the raw JSON data, then simply remove the json_decode
.
顺便说一句,如果您只想要原始 JSON 数据,那么只需删除json_decode
.
回答by T.Todua
1) local simplest methods
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) Better Way is CURL:
2)更好的方法是 CURL:
echo get_remote_data('http://example.com'); // GET request
echo get_remote_data('http://example.com', "var2=something&var3=blabla" ); // POST request
It automatically handles FOLLOWLOCATIONproblem + Remote urls: src="./imageblabla.png"
turned into:src="http://example.com/path/imageblabla.png"
它自动处理FOLLOWLOCATION问题 + Remote urls:src="./imageblabla.png"
变成:src="http://example.com/path/imageblabla.png"
Code : https://github.com/tazotodua/useful-php-scripts/blob/master/get-remote-url-content-data.php
代码:https: //github.com/tazotodua/useful-php-scripts/blob/master/get-remote-url-content-data.php
回答by Dr D
Don't forget: to get HTTPS contents, your OPENSSL extension should be enabled in your php.ini. (how to get contents of site use HTTPS)
不要忘记:要获取 HTTPS 内容,应该在 php.ini 中启用 OPENSSL 扩展。(如何使用HTTPS获取网站内容)
回答by cweiske
Use file_get_contents
in combination with json_decode
and echo
.
使用file_get_contents
结合json_decode
和echo
。
回答by Steve Mayne
$url = "https://chart.googleapis....";
$json = file_get_contents($url);
Now you can either echo the $json variable, if you just want to display the output, or you can decode it, and do something with it, like so:
现在你可以回显 $json 变量,如果你只想显示输出,或者你可以解码它,并用它做一些事情,像这样:
$data = json_decode($json);
var_dump($data);