2011-12-02 104 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