How to Easily Parse a RSS Feed with PHP 4 or PHP 5

Today I had to parse a Wordpress RSS feed for display on a customers website. The basic goal was to show the latest blog postings on the customer’s homepage without them having to manually feed in the data. I have done this before using some of the great XML tools in PHP 5 that make this kind of task a breeze. Unfortunately, the customer’s website resides on another company’s server and they only have PHP 4 available. It had been a long time since I had parsed an RSS feed for PHP 4 but I dug up this handy function I used to use frequently back in the PHP 4 days. It works in PHP 4 or PHP 5 and makes parsing an RSS feed a piece of cake. Basically it parses the feed and returns an array of the results.

Below is the sample code that shows how this is done. The first section has the parseRSS() function and the second part is an example of me calling the function on my blog RSS feed. If you copy the code straight away you should see in your browser the last 10 entries from your favorite blog… mine.

<?PHP
 
	/******************************************************************************************************************
	   RSS PARSING FUNCTION
	******************************************************************************************************************/
 
	//FUNCTION TO PARSE RSS IN PHP 4 OR PHP 4
	function parseRSS($url) { 
 
	//PARSE RSS FEED
        $feedeed = implode('', file($url));
        $parser = xml_parser_create();
        xml_parse_into_struct($parser, $feedeed, $valueals, $index);
        xml_parser_free($parser);
 
	//CONSTRUCT ARRAY
        foreach($valueals as $keyey => $valueal){
            if($valueal['type'] != 'cdata') {
                $item[$keyey] = $valueal;
			}
        }
 
        $i = 0;
 
        foreach($item as $key => $value){
 
            if($value['type'] == 'open') {
 
                $i++;
                $itemame[$i] = $value['tag'];
 
            } elseif($value['type'] == 'close') {
 
                $feed = $values[$i];
                $item = $itemame[$i];
                $i--;
 
                if(count($values[$i])>1){
                    $values[$i][$item][] = $feed;
                } else {
                    $values[$i][$item] = $feed;
                }
 
            } else {
                $values[$i][$value['tag']] = $value['value'];  
            }
        }
 
	//RETURN ARRAY VALUES
        return $values[0];
	} 
 
 
	/******************************************************************************************************************
	  SAMPLE USAGE OF FUNCTION
	******************************************************************************************************************/
 
	//PARSE THE RSS FEED INTO ARRAY
	$xml = parseRSS("http://www.stemkoski.com/?feed=rss2");
 
	//SAMPLE USAGE OF 
	foreach($xml['RSS']['CHANNEL']['ITEM'] as $item) {
	        echo("<p class=\"indexBoxNews\"><a href=\"{$item['LINK']}\" target=\"_blank\" class=\"indexBoxNews\">{$item['TITLE']}{$link}</a></p>");
	}
 
?>

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

effing perfect!

Hi there,
Thanks! I ‘ve been looking for a way to do this. I used to know a little php and my server uses old php4.

I get a strange character before my list : 

Is there a way I can easily limit the number of entries shown? To limit it to the most recent 3 entries?

Thanks,
Steve in Canada

[...] PHP4 as well because the client is on one of our older servers. I ended up finding a really good example by Ryan Stemkoski. He wrote a perfect function for me to tweak a little and expand upon. I am currently waiting to [...]

Hey Ryan:

I wanted to thank you for posting this code. It saved quite a bit of time trying to get started. I wanted to let you know I have modified your code a little bit to allow you to limit the number of items as well as only contain items that where the title matches certain keywords. I also turned my modifications into a WordPress plugin as well.

If you, or anyone else, would like to see my modifications or WordPress plugin please check out http://techno-geeks.org/2009/06/selective-rss-plugin-for-wordpress/

Leave a comment

(required)

(required)