file_get_contents()无法获取数据?
问题描述:
我搜索了几个与此类似的帖子,但找不到答案。我可能错过了一些小东西。file_get_contents()无法获取数据?
我想如我的代码从URL中读取JSON内容:
$uri = "http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
echo "URI: [$uri] <br/>";
$file = file_get_contents($uri);
$error = error_get_last();
echo $error['message'];
如果我在浏览器中打开URL,我可以看到它的JSON内容。但上面提到的代码不起作用。 $file
的值为false
。错误消息是: file_get_contents(http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710) [function.file-get-contents]: failed to open stream: No such file or directory.
有什么建议吗?注意:如果它很重要,我的allow_url_fopen
设置为1
。
编辑: 这是整个PHP文件。 http://pastebin.com/zCKEP9kG
另外,在打开一个http文件的代码中有一个fopen()就好了。
答
检查这个代码,我想你可以用这个,现在继续进行 -
<?php
$json_url = "http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
@ ASCII石灰
其@ ASCII-lime--
检查(编辑#2)
答
试试这个:
<?php
header('Content-Type: application/json');
$json = file_get_contents('http://worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710');
echo $json;
?>
你确定'allow_url_fopen'设置为'1'吗?如果你自己设置它,你可能需要重新启动你的web服务器(假设PHP在某个服务器上运行)。 – Paulpro
@ ascii-lime:是的,我确定。我通过ini_get()来检查它 – aakash