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>
<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
);
(
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.info</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.
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.
ReplyDeleteGenerating XML in this way is an awful idea. There's XML libraries for a reason.
ReplyDeleteYou're not doing an escaping either.
I agree with Ian, you need to escape your title and description of each item. You can use htmlentities or htmlspecialchars for that.
ReplyDeletehi ,
ReplyDeleteThis gives me a warning Error headers cannot modify ........
i have a problem with tamil feed generation. Please can you help me?
ReplyDelete[email protected]
tamada bujji really nice demos ....ramesh & ravi
ReplyDeleteyou can add rss+xml to display the data as rss feed as below:-
ReplyDelete@header("Content-type: text/rss+xml");
Great article, thanks!
ReplyDeletethis works..but nit teh best way :P
ReplyDeleteIs it allowed to use another encoding in rss feed instead of utf-8?
ReplyDeletei have a problem with create pubdate.
ReplyDeleteWhich should I use on a table? date or datetime? and how to use UTC?
thanks before...
i want to create a rss for job finding,, how can i create it.. plz any body tell me.........
ReplyDeletei have a problem...
ReplyDeletei want to create a rss feed for an job finding website,, how can i create it,,,
plz any tell me................
Please tell me the how to display image in it
ReplyDeletevery nice i recently made this in my website !!
ReplyDeletevery nice post
ReplyDeletethanks a lot man !!
ReplyDeleteis it possible to create rss feed without Mysql?
ReplyDeletethanks a lot!!!!!!!!!!
ReplyDeleteAny one help me? where I found blog site feed link?
header("Content-Type: application/rss+xml; charset=utf-8");
ReplyDeleteuse this code to header:
Insert the code in top of the page and then loop your data.
5ert
ReplyDeleteThanks for the tip.
ReplyDeleteThanks you!
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteThanks for the tip my friends ^_^
ReplyDeletePlease tell me the how to display image in it
ReplyDelete