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
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