Web Development Portfolio - Darren Peter Smith
 

Darren Peter Smith's Web Development Lab

RSS widget


Recently I have become interested in the potential of RSS feeds in web 2.0 as a way of syndicating content. As a project I decided to write an application which could act as an RSS feed reader.

The first step was to figure out how to parse the XML from RSS feeds and turn it into a useful form.

I adopted an object orientated approach with a Channel object which took a feed's address, contained the code to parse the rss and returned various information from the feed as output.

I used the xml_parser function of php to parse the feed and store the information in the Channel object:

$xml_parser = xml_parser_create(); 
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($xml_parser, $data, $values, $tags);
xml_parser_free($xml_parser); 

$numberOfTags = sizeof($values);
for($i = 0; $i <= $numberOfTags; $i+=1) {
		
	$entry = $values[$i];
	$tag = strtolower($entry['tag']);
		   
	if ($tag == "title") {
		$this->setTitle($entry['value']);
	}
	elseif($tag == "link"){
		$this->setLink($entry['value']);
	}
	...

The Channel object was an extension of an Item object and also contains an array of Items representing each item in the RSS feed.


class Item {
var $title;
var $link;
var $description;
	
function Item()
{
	$this -> dateadded = date("l M dS, Y, H:i:s",5678);
}

function setTitle($title)
{
	$this->title =$title;
}
function setLink($link)
{
	$this->link =$link;
}
function setDescription($description)
{
	$this->description =$description;
}
function getTitle()
{
	return $this -> title;
}
function getLink()
{
	return $this -> link;
}
function getDescription()
{
	return $this -> description;
}

function OutputItem()
{
	$os .= "<h3>";
	$os .= "<a href=". $this->getLink() ." >";
	$os .= $this->getTitle();
	$os .= "</a>";
	$os .= "</h3>";
	$os .= "<br />";
	$os .= $this->getDescription();
	return $os;
}  
}

This OO approach allowed me to take the code I was using to write a full RSS reader and use it to create a smaller application to display specific feeds specified by the page author.