HOME FORUM ARTICLES TUTORIALS SCRIPTS LINKS NEWS MENTORS TOOLS REGISTER

Coding RSS Feeds in PHP

an article by ERT Mentor Srirangan

Over the past few weeks while I was working for a news portal, we came across the need to create a RSS feeds generator in PHP. Now since I presumed many of the PHP programmers out there would also come across such demands at some point, I thought I'd document the development of a simple RSS feeds generator on Programmer Assist.

The first thing to understand is that 'RSS feeds' is nothing but a XML document. It is generally dynamically generated server side, often via databases. So, simplistically speaking, all that an RSS page consists in a bunch of XML tags and content. However like every other XML document, standards are very important even in RSS.

We decided to follow a simplistic and a minimalistic approach while development. We divided the RSS document into three parts, the header, the footer and the content in the middle. This is how we developed each one of the component.

1) The Header

Following the RSS 2.0 specifications, the structure of the header would look something like this:

<rss version="2.0">
<channel>
<title>Title</title>
<link>http://website.com</link>
<description>Description</description>
<category>rss, feeds, in, php</category>
<generator>me :-)</generator>
<webMaster>my@email.com</webMaster>

2) The Footer

The structure of the footer would resemble:

</channel>
</rss>

3) The Content

The content is ofcourse the main part. Each RSS feed item is created an <item> tag which has a range of self explanatory sub tags. An example is given below:

<item>
<title>item title</title>
<link>item link</link>
<description>item description</description>
<author>item author</author>
<category>item category</category>
</item>

Now since we are familiar with the basic structure of an RSS 2.0 document, we can now proceed generating this via PHP.

The the step in the PHP code is to specify the document content-type and encoding. These two lines would do just the same:

header("Content-type: text/xml \r\n");
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';

Then we move on to display the RSS header:

echo '
<rss version="2.0">
<channel>
<title>Title</title>
<link>http://website.com</link>
<description>Description</description>
<category>rss, feeds, in, php</category>
<generator>me :-)</generator>
<webMaster>my@email.com</webMaster>
';

Then comes the content part of the RSS document, this is where the item's (following the structure explained above) are dynamically generated out of your database/content. It would look a bit like this:

for( ..looping..through..your..content.. )
{
echo '
<item>
<title>'.$item-title-var.'</title>
<link>'.$item-link-var.'</link>
<description>'.$item-description-var.'</description>
<author>'.$item-author-var.'</author>
<category>'.$item-category-var.'</category>
</item>
';
}

Please note the code to loop through your relevant content and your appropriate variables can not be prescribed by me in this tutorial. It is for you to decide what to publish, it is your RSS Feed after all! :-)

Then comes the footer part of the RSS document, which would be implemented as follows:

echo '
</channel>
</rss>
';

So placing all of this together, your PHP code would look something like:

<?php
header("Content-type: text/xml \r\n");
echo '<?xml version="1.0" encoding="ISO-8859-1"?>';

echo '
<rss version="2.0">
<channel>
<title>Title</title>
<link>http://website.com</link>
<description>Description</description>
<category>rss, feeds, in, php</category>
<generator>me :-)</generator>
<webMaster>my@email.com</webMaster>
';

for( ..looping..through..your..content.. )
{
echo '
<item>
<title>'.$item-title-var.'</title>
<link>'.$item-link-var.'</link>
<description>'.$item-description-var.'</description>
<author>'.$item-author-var.'</author>
<category>'.$item-category-var.'</category>
</item>
';
}

echo '
</channel>
</rss>
';
?>

Can't. Get. Simpler. smile Happy Coding!

© Copyright Srirangan, 2005-2007
post to Dzone Digg this! Add to del.icio.us Googleize this Add to reddit Save to myYahoo Add to furl Add to Netvouz! Spurl this! Add to Linkroll! Save to Simpy Give if thumbs up on StumbleUpon Save to Blinklist Add to Tektag Save to Bibsonomy Submit to Tweako
Search ERT on the Tools Page

Did you know? You can discuss this article with the mentor who wrote it and others interested in the topic? You are invited to join the discussion with Go to the forum

Got a technical article or tutorial you want to publish on the Internet? Join Go to the forum in the Round Table Forum and let the Mentors know what you have. If it meets ERT standards, is factual and can help ERT visitors, then ERT Mentors and Editors can help you (without charge) polish your offering so it can be published and promoted by ERT. An article published on ERT may be read by as many as 10,000 visitors a week; promoting you, your site, and your ideas. Please note ERT does not publish re-prints; promotional handouts, or pieces consisting mainly of links. So original technical content only please. If you prefer you can email the Editor