General
Integrating/Reading RSS Feed Using PHP
March 6, 2010
0

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.

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

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.

<?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."";
?>

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.

<?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>';
?>

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.

<?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>."
";
}
?>

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.

<?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>";
}
?>

Formating Nicely

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

<?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>
";
}
?>

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.