A hack to parse RSS feeds with php

Just happened to assemble this script hack recently, out of the requirement for a quick'n'dirty feed parsing feature:


$feed_contents = file_get_contents($feed_url);
$xml = simplexml_load_string($feed_contents, LIBXML_NOCDATA); 
$feed_array = json_decode(json_encode($xml));
print_r($feed_array); // Surprise!!

Now this evidently is not SimplePie or Magpie RSS or whatever feed reader library tickles your fancy*, but assuming the feed will never be malformed, it can save you lots of time! Isn't that what php was meant for? :-P At first it surprised me that it would even work--but it seems that the JSON module interoperates very nicely with SimpleXML objects. At the end you get an associative array in $feed_array which is a nice representation of the RSS 2 feed. It's important to highlight the LIBXML_NOCDATA parameter, as it makes sure the CDATA nodes are read as text. Otherwise you would just get empty feed items, etc.

Obviously for this to work you need both the JSON and SimpleXML modules installed and enabled in your server. Since that's something that always worries me, I wrote another quick script to test whether the hack would work on the server:


$functions = array('simplexml_load_string', 'json_decode');
foreach($functions as $f) {
  echo $f . ' exists? ' . (function_exists($f) ? 'yes' : 'no') . '<br />';
}

It should output something like...


simplexml_load_string yes
json_decode yes

Happy hacking! :-P