2012-11-26 276 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:檢查我的答案,並正確 –