Tuesday, 11 November 2008

Quick and easy way to make a RSS Reader

The quickest way to build a custom RSS reader in PHP is to use the Simple XML library. The library comes with the standard PHP installation but requires you to compile PHP with the extension enabled. More information about Simple XML can be found here.

Here is some simple code to use:
// define feed url
$feed_url = "http://www.xtremeps3.com/rss.php";

// read in xml file
$data = file_get_contents( $feed_url );

// convert xml to objects
$data_array = simplexml_load_string( $data );

// spit out objects
echo '<pre>'.print_r( $data_array, true ).'</pre>';

You can easily put this code into a function to load a user defined RSS feed. To access the elements in the object, you can use $data_array->channel->item->title, for example, to access the title of the first entry in the feed.

The ideal way would be to loop through the $data_array->channel->item object using a foreach loop and output the data you need.

Labels: , , ,