How to Integrate an RSS Feed Into Your Site with Simple XML
Practically every site today has an RSS feed. Now, you could be like everyone else and simply use this as a way to read information as it’s published.
Or, you could use RSS for what’s intended for - content syndication. With Simple XML, it’s a piece of cake to use an RSS feed to add a list of links to relevant articles on your page.
What Are RSS Feeds and XML Files?
Hopefully you know what an XML file and an RSS feed is. If not, here’s the quick and dirty explanation.
An XML file is a special way to store information. All of the information is wrapped in tags - kind of like HTML. However, instead of using this to format the output, this is used to tell the reader what the information means.
So, for example, a <link> tag would probably describe a link.
XML was created to allow information to be easily ported between platforms and systems. An RSS feed is a special type of XML file to help reach that goal. Most websites (including almost all blogs), publish an RSS feed that is simply an XML file containing the latest few entries on the website.
An RSS feed also has a standard structure - which is why we can easily build a script that can parse any valid RSS feed and add the links to your site.
Getting Started with Simple XML
Before SimpleXML, you had to build your own XML parser to work with PHP and XML files. This was possible, but it meant a lot of overhead - so it wasn’t a good idea for small projects.
With SimpleXML, you can load any XML file into PHP as a specially structured object. The object’s structure mirrors the XML file. So, if there’s a <item> tag, it creates an object with name “item.” It then creates children objects of every tag inside that item tag.
To load an XML file into a SimpleXML object, you simply use this function call. Run this entire script to see what the SimpleXML object looks like after it’s loaded.
$filename = "http://www.dzone.com/links/feed/frontpage/rss.xml"; // That's the DZone RSS Feed $feed = simplexml_load_file($filename); var_dump($feed);
What’s In an RSS Feed, and How Do We Use It?
If you ran that script, you should see the guts of an RSS feed. I’d suggest you view the source code of the output page, since it’ll be formatted nicely for you.
Like I mentioned before, RSS feeds have a standard structure so that apps can predictably interact with them. This is the basic structure you should see inside the DZone RSS feed.
// Some random stuff up top <channel> // This parent tag holds all the page information <item> // There's one item per page listed in the feed <title> ... </title> <link> ... </link> <description> ... </description> <pubDate> ... </pubDate> </item> // You could have a lot more <item> tags here </channel>
All of this information is nicely placed in our SimpleXML object for us to manipulate.
The entire channel (all of the children tags inside the channel tag) is stored in the ‘channel’ object, which is a child of the larger ‘feed’ object that we created earlier ($feed).
You can access the channel like this.
var_dump ($feed->channel);
If there were multiple channels, then ‘channel’ wouldn’t be a single object - it would be an array of objects. This is the case with the ‘item’ tags. There are a lot of items, so they are stored in an array - which is a child object of the ‘channel.’
You can access the items like this.
var_dump ($feed->channel->item[n]);
Finally, the individual item holds most of the information we want. It has children named ‘pubDate,’ ‘title,’ and ‘link’ (along with a few other optional parameters).
echo $feed->channel->item[0]->title; echo $feed->channel->item[0]->link; echo $feed->channel->item[0]->pubDate;
So How Is This Useful to Us?
As you may have noticed, each item consists of the information you need to make a link back to it. The item consists of a title, a URL, a pubDate, and a description. You could use any combination of these that you feel is useful - but here’s an example of how to create a link to an article.
echo '<a href="' . $feed->channel->item[0]->link . '">' . $feed->channel->item[0]->title . '</a>';
And that’s all there is to it. You could spruce it up by adding a date underneath the link. Or you could add a short excerpt of the description.
What If I Want to Include All of the Links?
I suppose adding one link is kind of silly. With a loop, we can easily iterate through the item[] array and output a link for every item in the RSS feed.
For good measure, we’ll also format it nicely in an unordered list. From there, you can add some styling and it’ll fit right into the sidebar of your page.
// Load the XML file into a Simple XML object $filename = "http://www.dzone.com/links/feed/frontpage/rss.xml"; $feed = simplexml_load_file($filename); // Iterate through the list and create the ul echo "<ul>"; foreach ($feed->channel->item as $item) { echo "<li><a href='" . $item->link . "'>" . $item->title . "</a></li>"; } echo "</ul>";
There ya have it. A semantically structured list, built from a remote RSS feed. All in a couple lines of code.
You could of course separate this out into a function, and then pass a filename (and perhaps a limiting # of items to print) to your function. The function would return a string with the html list, or output it directly (depending on your preference).
Word of Caution: Remember that you don’t own content just because it is published in an RSS feed. The author still does.
I would assume that it’s fair game and free of copyright issues to print links to the original author’s site. However, some authors publish their entire articles in their RSS feeds. Do not re-publish the entire article. That is copyright infringement, it is illegal, and you can get in a lot of trouble. Not to mention it’s highly unethical.
With that in mind, happy syndicating!







Syndicate RSS Feeds on your Website for Extra Content | Review of Information about Web Design said this on February 20th, 2008 at 2:20 pm
[…] need to know is how to use SimpleXML and how an RSS feed is structured. Here’s a tutorial on how to syndicate an RSS feed in PHP, if you’re interested in learning how to do that. Use Rel=’nofollow’ In the […]
EH said this on April 24th, 2008 at 3:49 am
Just what I needed - small simple code! Now need multiple feeds and sorting!
Share Your Reading Habits: Google Reader | Web Cash said this on April 28th, 2008 at 8:53 pm
[…] feed is available in a standard rss xml file. You could use SimpleXML to parse the feed and display the information yourself. Or, you could use the ready-made widget that Google […]