9lessons programming blog
Loading Search
Saturday, March 28, 2009

Generate RSS Feed with PHP.

This post explains how to create RSS feed for web projects with PHP. It's simple code just you have to include the basic RSS stucture in while loop.




Basic structure of RSS feed.
<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>

<title>Website Title </title>
<link>Website URL</link>
<description>Website description</description>
<language>en-us</language>

<item>
<title>Item/Article Title</title>
<link>Item/Article URL </link>
<description>Items/Article description </description>
</item>

</channel>
</rss>

Sample Database Table.
CREATE TABLE site_data
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(250),
link VARCHAR(250),
description TEXT
);

rss.php
Code contains PHP and XML RSS basic structure tags. So your feed link http://domain.com/rss.php.
<?php
include('db.php');
$sql = "SELECT * FROM site_data ORDER BY id DESC LIMIT 20";
$query = mysql_query($sql) or die(mysql_error());

header("Content-type: text/xml");

echo "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>9lessons | Programming Blog </title>
<link>http://9lessons.blogspot.com</link>
<description>Programming Blog </description>
<language>en-us</language>";

while($row = mysql_fetch_array($query))
{
$title=$row['title'];
$link=$row['link'];
$description=$row['description'];

echo "<item>
<title>$title</title>
<link>$link</link>
<description>$description</description>
</item>";
}
echo "</channel></rss>";
?>

Related Posts :

Reading and Displaying RSS Feed with PHP.
Sponsored Links

Share this post

Comments
{ 10 comments }
Phog said...

I forgot exactly how, but you can also have PHP change the document header to xml instead of whatever the default is. This is useful when you want an rss reader to detect that a file is xml instead of html.

Ian P. Christian said...

Generating XML in this way is an awful idea. There's XML libraries for a reason.
You're not doing an escaping either.

cpradio said...

I agree with Ian, you need to escape your title and description of each item. You can use htmlentities or htmlspecialchars for that.

ipl said...

hi ,
This gives me a warning Error headers cannot modify ........

Elango said...

i have a problem with tamil feed generation. Please can you help me?

nelamurugan@gmail.com

Anonymous said...

tamada bujji really nice demos ....ramesh & ravi

Anonymous said...

you can add rss+xml to display the data as rss feed as below:-
@header("Content-type: text/rss+xml");

Coupons said...

Great article, thanks!

Anonymous said...

this works..but nit teh best way :P

Biznesa Plans said...

Is it allowed to use another encoding in rss feed instead of utf-8?

Post a Comment

Subscribe now!Recent Posts

Categories

Subscribe now!Popular Posts

People Says

@9lessons thank you for the great tutorials, we truly appreciate your contributions to the design community.

Smashing Magazine

Like Me

follow me
products

9lessons labs

9lessons clouds

Android application

Chrome Extension