2010-07-08 42 views
0

我想從一個沒有跨域文件的域使用RSS源,並且因爲這樣我將在中間使用一個Web服務,我將只從一個url獲取RSS源(讓我們假設網址是:www.example.com/feed),然後將其打印到一個頁面。使用PHP打印rss?

該服務的工作原理是:www.mywebservice.com/feed.php?word=something),並且只會打印rss提要:www.example.com/feed & q = word)。

我用:

<?php 

$word = $_GET["word"]; 

$ch=curl_init("http://example.com/feed.php?word=".$word.""); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

$data = curl_exec($ch); 
curl_close($ch); 

print $data; 

?> 

但這並沒有工作,它給了我(系統錯誤:很抱歉,但是已經出現在系統中一個嚴重的錯誤)。我在共享主機 任何幫助?

+2

您有具體問題嗎? – AndrewC 2010-07-08 16:55:01

+0

我不知道如何從網站獲取xml提要然後打印它,所以開始時,獲取xml提要將是一個好的開始,然後將其打印爲:print $ xml? – user220755 2010-07-08 16:55:45

回答

0

所以在最後我終於實現了這一點:

$word = $_GET["word"]; 

$url = "http://www.example.com/feed.php?q=".$word; 

$curl = @curl_init ($url); 

@curl_setopt ($curl, CURLOPT_HEADER, FALSE); 
@curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE); 
@curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, TRUE); 
@curl_setopt ($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 

$source = @curl_exec ($curl); 
@curl_close ($curl); 
print $source; 

我希望這被認爲是一個答案不是編輯(如果編輯請告訴我,這樣我就可以刪除這個答案和編輯文章)

2

readfile函數讀取文件並將其寫入輸出緩衝區。

readfile('http://example.com/feed.rss'); 

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the List of Supported Protocols/Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

如果你需要用XML做任何事情,使用PHP's many XML libraries一個,最好DOM,但也有SimpleXmlXMLReader。作爲替代,您可以使用Zend_Feed from the Zend Framework作爲獨立組件來使用RSS提要。

如果您不能在您的服務器上啓用allow_url_fopen,或者try cURL like Matchu suggestedgo with Artefacto's suggestion

+0

讀取文件的問題是它給了我錯誤(在服務器配置中禁用URL文件訪問)。我試過但不知道如何修正配置或者實際上使用別的 – user220755 2010-07-08 16:59:06

+1

@use你必須啓用allow_url_fopen或者用fsockopen/curl解決它。 – Artefacto 2010-07-08 17:01:34

+0

請看上面編輯過的帖子,感謝您的幫助! – user220755 2010-07-08 17:08:58

0

如果您還想解析XML並進一步處理它,請務必查看SimpleXML。讓我們來解析並操縱Feed。

1

既然您說由於服務器限制您無法執行花哨的URL文件打開快捷方式,您將需要使用PHP's cURL module來發送HTTP請求。

+0

我正在使用它(請看看上面編輯過的文章),但由於某種原因它不工作。 – user220755 2010-07-08 17:09:13

+0

@ user220755 - 聽起來像錯誤實際上是嚴重的。該代碼看起來像它應該工作。嘗試與您的託管服務提供商討論。 – Matchu 2010-07-08 17:23:43