2014-05-22 54 views
1

我一直在使用PHP的simplexml_load_file解析大量的RSS提要,它就像一個魅力。 現在我正在嘗試爲金融時報的RSS提要做同樣的事情。當我做...如何在PHP中解析RSS非XML Feed?

$rss = simplexml_load_file("http://www.ft.com/rss/world"); 

...我得到:

Warning: simplexml_load_file(): http://www.ft.com/rss/world:11: parser error : Opening and ending tag mismatch: link line 8 and head in rss.php on line 6 

Warning: simplexml_load_file(): oat:left;margin-right:20px;margin-top:3px;width:35px;height:31px;}</style></head in rss.php on line 6 

Warning: simplexml_load_file():^in rss.php on line 6 

Warning: simplexml_load_file(): http://www.ft.com/rss/world:37: parser error : Opening and ending tag mismatch: input line 37 and li in rss.php on line 6 

Warning: simplexml_load_file():^in rss.php on line 6 

和很多很多的警告(約100)。

我已經搜索了Stackoverflow的答案,但我找不到任何似乎適用於這種情況。我在這裏錯過了什麼?

回答

0

你的代碼適合我。嘗試省略LIBXML_NOWARNING & LIBXML_NOERROR(這會抑制您可能遇到的任何錯誤),以查看錯誤發生的位置。

+0

您是否嘗試過使用FT feed?我省略了LIBXML擴展,但它仍然是一樣的。 vardump返回false。請注意,此代碼適用於大多數其他Feed ... – TheBigDoubleA

+1

您似乎從URL獲取HTML。嘗試使用file_get_contents獲取它並回顯它以查看您的Web服務器正在接收的內容。也許他們正在過濾一些用戶代理以獲取其供稿。 – Othi

+0

你是對的:我得到一個html頁面,這是一個:http://www.ft.com/gfdlgjfdglkfjdgd。我該如何克服這一點? – TheBigDoubleA

1

要使一些網站正常工作,您需要使用HTTP請求設置用戶代理。由於PHP中的默認設置可能是空的(這似乎是一個理智的設置隱私智慧),您需要設置它的請求:

ini_set('user_agent', "Godzilla/42.4 (Gabba Gandalf Client 7.3; C128; Z80) Lord of the RSS Weed Edition (KHTML, like Gold Dust Day Gecko) Chrome/97.0.43043.0 Safari/1337.42"); 

$rss = simplexml_load_file("http://www.ft.com/rss/world");