Generate RSS Feed with PHP.
Wall Script
Wall Script
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.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.
web notification

26 comments:

  1. 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.

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

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

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

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

    [email protected]

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

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

    ReplyDelete
  8. this works..but nit teh best way :P

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

    ReplyDelete
  10. i have a problem with create pubdate.
    Which should I use on a table? date or datetime? and how to use UTC?
    thanks before...

    ReplyDelete
  11. i want to create a rss for job finding,, how can i create it.. plz any body tell me.........

    ReplyDelete
  12. i have a problem...

    i want to create a rss feed for an job finding website,, how can i create it,,,
    plz any tell me................

    ReplyDelete
  13. Please tell me the how to display image in it

    ReplyDelete
  14. very nice i recently made this in my website !!

    ReplyDelete
  15. is it possible to create rss feed without Mysql?

    ReplyDelete
  16. thanks a lot!!!!!!!!!!
    Any one help me? where I found blog site feed link?

    ReplyDelete
  17. header("Content-Type: application/rss+xml; charset=utf-8");

    use this code to header:

    Insert the code in top of the page and then loop your data.

    ReplyDelete
  18. Thanks for the tip.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. Thanks for the tip my friends ^_^

    ReplyDelete
  21. Please tell me the how to display image in it

    ReplyDelete

mailxengine Youtueb channel
Make in India
X