Search code examples
phprsssimplexml

SimpleXmlElement throws exception, after having worked fine for over a year


A few years ago I made a simple RSS to SSS converter (so I could get my RSS feed into a format that Second Life's LSL could handle). It's been working fine for over a year, until a few days ago (1 May to be exact). Since then, it's been throwing an error.

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/[username hidden]/public_html/API/rss2sss.php:4 Stack trace: #0 /home/[username hidden]/public_html/API/rss2sss.php(4): SimpleXMLElement->__construct('') #1 {main} thrown in /home/[username hidden]/public_html/API/rss2sss.php on line 4

The code in rss2sss.php:

<?php
$feedUrl = $_GET['rss'];
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed); /*This is where the exception happens*/


$channel['title'] = $xml->channel->title;
$channel['link']  = $xml->channel->link;

echo '<sss>';

foreach ($xml->channel->item as $item)
{    

    $article = array();
    $article['title'] = $item->title;
    $article['link'] = $item->link;

    echo '
    <pre>
        <title>';
    echo $article['title'] .'</title>
        <link>';
    echo $article['link'];
    echo '</link>
    </pre>';
}

echo '
</sss>';
?>

The RSS I'm trying to convert has not changed format in years, and is taken from this feed.

Any help at all in finding out why the code suddenly broke would be greatly appreciated, as it is affecting the visit rate of the site quite significantly.


Solution

  • I am still not sure what happened or where things got wrong, but the fault was not with the PHP, but with the RSS feed I was trying to pull.

    After a lot of cache purging and fiddling with the settings (nothing with formatting, only how many items should be visible in the feed), it suddenly started working as intended again. I deduced that it was the plugin W3Cache for Wordpress that caused the problem. Don't know why it started now, after having worked for so long, but disabling that plugin (after purging all caches one last time (important step)) fixed that problem.

    Still, don't know what exactly was wrong with the formatting of the XML, did a text compare between both the old feed and the new, working one and the only differences are the time codes from latest update, nothing in the formatting.