2011-12-02 102 views
1

我正在以下PHP致命錯誤RSS粉狀飼料向上死亡

和錯誤日誌指向線

$xml = new SimpleXMLElement($data); 

(這是非常下一行代碼後除了下面)作爲罪魁禍首。

但是,當我單獨運行每個饋送時,沒有錯誤,饋送保存到數據庫。

這是產生誤差的代碼:

$feeds = array( 
    'http://www.mtv.com/rss/news/news_full.jhtml', 
    'http://www.musicweek.com/rss.asp?navcode=232', 
    'http://www.cmt.com/rss/news/latestcached.jhtml', 
    'http://www.billboard.com/rss/news', 
); 

foreach ($feeds as $feed) 
{ 
    $ch = curl_init(); 

    // causes error: 
    curl_setopt($ch, CURLOPT_URL, $feed); 

    // works: 
    curl_setopt($ch, CURLOPT_URL, 'http://www.billboard.com/rss/news'); 

    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $data = curl_exec($ch); // put data from rss url into variable 
    curl_close($ch); 
    ... 

如果我切換註釋行和使用$飼料變量,錯誤和白頁。今天下午之前它工作的很好,所以我懷疑在觸發這個事件的Feed中有一些XML是非法的。

有沒有更好的方法來寫這個 - 或者 - 如果有必要的話可以捕獲異常?

回答

2

罪魁禍首是http://www.mtv.com/rss/news/news_full.jhtml。如果你加載頁面,你會得到一個可愛的錯誤。下面是Chrome的:

This page contains the following errors:

error on line 296 at column 38: Opening and ending tag mismatch: shorthead line 0 and i Below is a rendering of the page up to the first error.

有問題的故障線路目前寫着:

<shorthead>Big K.R.I.T. Promises </i>Live From The Underground<i> In Early 2012</shorthead> 

不奇怪,它失敗的。


至於捕捉錯誤,換你的代碼

try { 
    //... your code ... 
} catch(Exception $exception){ 
    //. . . Do somethign with exception ... 
} 
+0

謝謝!我使用Safari和Fireworks,但似乎我應該使用Chrome。 – jgravois