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?
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:
The faulty line in question currently reads:
No surprise that it failed at all.
As for catching the error, wrap your code in