Mar
06
2010

 

There are built in library to do RSS Feeds, but won’t it be nice to build our own and be able to customize the output.  PHP 5 or later is needed for this method because it uses SimpleXML.

Below are some useful links that we use in this tutorial.

Reading The RSS File

Using SimpleXML, the code below read the RSS file and outputs the XML structure.  This example does not do much but it demonstrates that the file is being read and parsed.

[c]
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo var_dump($xmlData);;
?>
[/c]

The result is below:

Displaying The Data

For starter, let’d siaplay the title, copyright, and the date the RSS file is generated. The following code reads the file, then displays the title, lastBuildDate, and copyright.

[c]
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo "
<h2>".$xmlData->channel->title."</h2>
";
echo "
<h3>Updated at ".$xmlData->channel->lastBuildDate."</h3>
";
echo "Copyright ".$xmlData->channel->copyright."";
?>
[/c]

The result is below:

As you see, it’s very straightforward.

Displaying The Logo

There’s an image node in the RSS, let’s display it.

[c]
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo ‘<img src="’.$xmlData->channel->image->url.
‘" width=’.$xmlData->channel->image->width.
‘" height=’.$xmlData->channel->image->height.’ ></img>’;
?>
[/c]

Displaying The News Items

If you examine the XML structure, you can see that the items are within the channel node. To display the item, we can iterate all of them. Below is an example of showing the title of each item title.

[c]
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo ‘<img src="’.$xmlData->channel->image->url.
‘" width=’.$xmlData->channel->image->width.
‘" height=’.$xmlData->channel->image->height.’ ></img>’;
echo "
";
for ($i=0; $i<count($xmlData->channel->item); $i++)
{
echo "Item ".$i.":".$xmlData->channel->item[$i]-><strong>title</strong>."
";
}
?>
[/c]

Linking to the News Item

Linking can be done by making each item a link, using link node. And while at it, let’s display the pubDate (publication date) too.

[c]
<?php
for ($i=0; $i<count($xmlData->channel->item); $i++)
{
$link=$xmlData->channel->item[$i]-><strong>link</strong>;
$title=$xmlData->channel->item[$i]->title;
$publicationDate=$xmlData->channel->item[$i]-><strong>pubDate</strong>;
echo "
<h2><a target=’_blank’ href=’".$link."’>".$title."</a></h2>
";
echo "
<h3>".$publicationDate."</h3>
<hr>";
}
?>
[/c]

Formating Nicely

Now that you know the basic, the formatting can be made nicer. Here’s an example:

[c]
<?php
$xmlData=simplexml_load_file("http://www.businessweek.com/rss/technology.rss");
echo ‘<img src="’.$xmlData->channel->image->url.
‘" width=’.$xmlData->channel->image->width.
‘" height=’.$xmlData->channel->image->height.’ ></img>’;
echo "
";
for ($i=0; $i<count($xmlData->channel->item); $i++)
{
$link=$xmlData->channel->item[$i]->link;
$title=$xmlData->channel->item[$i]->title;
$publicationDate=$xmlData->channel->item[$i]->pubDate;
echo "
<div class=’newsItem’><a target=’_blank’ href=’".$link."’>".$title."</a>";
echo "
<div class=’pubDate’>".$publicationDate."</div>
</div>
";
}
?>
[/c]

Result:


And that concludes this tutorial. It’s quite straightforward, isn’t it? PS: remember to check the RSS owner’s usage terms before using them.
Note: I am not in any way related to BusinessWeek, I am using their RSS only for illustration purpose.

Comments are closed.