2013-06-11 31 views
1

我試圖使用php加載存儲在 https://www.nextbigsound.com/charts/social50.rss的數據。使用PHP打開以.rss結尾的網址

我試過使用捲曲和simplexml_load_file函數,但這些都沒有爲我工作。我對這門語言很陌生,但當我按照這個鏈接顯示的數據看起來不過是一個xml文檔。不過,無論何時我嘗試加載網址,我都會收到一條錯誤消息,指出加載失敗。任何想法我做錯了什麼?

編輯: 這是我使用simplexml_load_file

$rss = simplexml_load_file('https://www.nextbigsound.com/charts/social50.xml'); 

試過當我想我用這個捲曲方法行:

$url = 'https://www.nextbigsound.com/charts/social50.xml'; 
$ch = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$rss = curl_exec($curl); 
curl_close($curl); 

要檢查是否被返回的結果我剛跑了簡單的var_dump($rss),但它總是顯示爲一個布爾值設置爲false。

+0

您的代碼在哪裏? – Baba

+0

http://net.tutsplus.com/articles/news/how-to-read-an-rss-feed-with-php-screencast/ – Hackerman

回答

1

簡單file_get_contents將工作

$content = file_get_contents('https://www.nextbigsound.com/charts/social50.rss'); 
echo $content; 

或使用simple_xml_load_string

$data = file_get_contents('https://www.nextbigsound.com/charts/social50.rss'); 
$xml = simplexml_load_string($data); 

上面的代碼假定有一個https包裝,你可以通過這個檢查:

echo 'https wrapper: ', in_array(stream_get_wrappers(), $w) ? 'yes':'no', "\n"; 

如果輸出false您需要撥打php_openssl擴展名。這可以通過在您的代碼中打開行來實現php.ini

extension=php_openssl.dll // on windows machine 
extension=php_openssl.so // on unix machine 
+0

當我運行這段代碼時,我得到如下警告:「Warning:file_get_contents ():無法找到包裝器「https」 - 您是否忘記在配置PHP時啓用它?「作爲file_get_contents的結果,我仍然收到false。有沒有應該省略的網址的任何部分? –

+0

你在哪裏運行這個腳本?在您的本地開發機器或某些託管? –

+0

在本地開發機器上。我嘗試過使用其他網址來獲得這些信息。有沒有什麼辦法可以阻止其他機器訪問它? –