THE ZEPINT NETWORK

programmer assist

PHP PHP XML Feeds

PHP Questions PHP Solutions PHP Articles

PHP is a popular open-source programming language used primarily for developing server-side applications and dynamic web content, and more recently, other software. The name is a recursive acronym for "PHP: Hypertext Preprocessor".

Create an RSS feeds generator in PHP

DiggBlinkRedditDeliciousTechnorati

article by 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'ld document the development of a simple RSS feeds generator on Programmer Assist (http://programmerassist.com).

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.

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>


The Footer

The structure of the footer would resemble:

</channel>
</rss>


The Content

The content is ofcourse the main part. Each RSS feed item is created by a <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 basic 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 ");
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");
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. :-) Happy Coding!

Got a PHP Question?

Just Sign Up and ask the top PHP experts!

Search via Google

User Login

Email Address

Password

PHP Experts

Rank Expert Points
#1 Srirangan 2650
#2 Anurag 600
#3 mastercomputers 100
This a list of the Top PHP experts, how many points do you have?

Leading Experts

Rank Expert Points
#1 frankzzsword 4600
#2 Bejaan 2900
#3 csfreak 1100
#4 Anurag 700
#5 keyvez 700
#6 nnarasimha 600
#7 Nakata 600
#8 martinig 600
#9 mastercomputers 400
#10 Huntress 150
#11 Adkron 150
#12 Yogesh 100
#13 lexxwern 100
#14 Mustan Khan 100
#15 Mahesh Kumar 100
This is a list of overall best performing experts, how many points do you have?