Reputation: 2579
I am getting the following PHP Fatal Error
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in Stack trace ...
And the error log is pointing to the line
$xml = new SimpleXMLElement($data);
(which is the very next line after the code except below) as the culprit.
However, when I run each feed individually, there is no error and the feed saves to the database.
This is the code that produces the error:
$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);
...
If I switch the commented lines and use the $feed variable, error and white page. It worked fine before this afternoon, so I am suspecting that there is something that is XML illegal in one of the feeds triggering this.
Is there a better way to write this -or- some way to catch the exception if necessary?
Upvotes: 1
Views: 3272
Reputation: 19552
The culprit is http://www.mtv.com/rss/news/news_full.jhtml . If you load the page, you get a lovely error. Here's Chrome's:
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.
The faulty line in question currently reads:
<shorthead>Big K.R.I.T. Promises </i>Live From The Underground<i> In Early 2012</shorthead>
No surprise that it failed at all.
As for catching the error, wrap your code in
try {
//... your code ...
} catch(Exception $exception){
//. . . Do somethign with exception ...
}
Upvotes: 2