2012-11-26 279 views
1

我搜索了几个与此类似的帖子,但找不到答案。我可能错过了一些小东西。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()就好了。

+2

你确定'allow_url_fopen'设置为'1'吗?如果你自己设置它,你可能需要重新启动你的web服务器(假设PHP在某个服务器上运行)。 – Paulpro

+0

@ ascii-lime:是的,我确定。我通过ini_get()来检查它 – aakash

回答

2

我看不出有什么毛病代码,队友 - 它运行良好本地上我的设置,我可以得到的文件,并可以做任何我喜欢它。

试试它运行在不同的主机上 - 可能是你的php.ini搞砸了,并且搞乱了脚本。或者你可能有一些其他地方的源问题..可能是如果你共享整个文件源,或者至少有一大块它..

干杯!

+0

我已经添加了整个PHP文件。请注意,在代码开头调用的fopen()可以正常工作。 – aakash

+0

我试着在不同的主机上运行它。有效。也许,正如你所说,我的php.ini搞砸了。 – aakash

-1

检查这个代码,我想你可以用这个,现在继续进行 -

<?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石灰

enter image description here

其@ ASCII-lime--

检查(编辑#2)

Edit 2

+1

如果'file_get_contents'正在读取文件,那么这不起作用。 – Paulpro

+1

@ ascii-lime我至少在我的最后测试了这个代码,它的工作检查截图 – swapnesh

+0

是的,它为你工作,因为'file_get_contents'为你工作。 OP的原始代码也适用于您。 – Paulpro

-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; 
    ?> 
+0

@aakash:我的代码正在工作。为什么负面投票? –

+0

我没有投票。 – aakash

+0

@aakash:检查我的答案,并正确 –