Creating Dynamic RSS Feeds With PHP
Date: 08/02/2005
Author: Wayne Eggert
Difficulty: Novice
Introduction
RSS or Really Simple Syndication
is everywhere. It's actually getting to be uncommon to find
news-related websites that don't have an RSS feed available. So how can
you get in on the action and start offering dynamic RSS feeds on your
website? It's simple really.. if you're using Apache as your web
server, you need to tell it to process ".rss" files as PHP files. You
then need to create the code to dynamically generate the RSS feed. For
low-to-medium traffic websites, creating the feed on-the-fly should not
be very resource intensive.. if you have a high traffic site tho,
you'll likely want to create a static RSS file on a certain time
interval so your database isn't running queries each time someone makes
a request.
Create or Modify Your .htaccess File
You'll
first need to create (or modify) your .htaccess file. If you haven't
worked with .htaccess files before, just know that they are an easy way
to configure Apache for local-instances of a configuration. So instead
of making a global configuration change to Apache that would affect all
sites running on the server, the .htaccess file allows you to modify
how a single site or directory works.
Add this line to your .htaccess file:
Creating the Database
If you don't already have a database table with the information you
want to include in the RSS feed, you can use the SQL code below to
create an example table. After creating the table, insert a few records
through PHPMyAdmin or through some PHP SQL calls.
`rssID` int(10) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`date` date NOT NULL default '0000-00-00' ) TYPE=MyISAM;
Example of what to enter in this database table for each domain you want to check:
rssID - Leave Blank
title - "Techdose.com brings you dynamic RSS!"
description - "Today you are witness to the great marvel of dynamic RSS feeds. Congratulations!"
date - 2005-08-02
Create the RSS File
At this step you should have a database table with some data in it. Now
it's time to create the dynamic RSS file to pull the data from the
database. Copy the code below into a new file and save it as
"rsstest.rss". Make sure to add in your database include or connection
code to replace the first line.
<? // @@@ YOUR DATABASE CONNECTION INCLUDE HERE @@@ ?>
<? header('Content-type: text/xml'); ?>
<? echo "<?";?>xml version="1.0" encoding="iso-8859-1"<? echo "?>";?>
<rss version="2.0">
<channel>
<title>RSS Test Feed</title>
<description>This is the description of the test feed.</description>
<link>http://www.yourdomain.com</link>
<?
$sql = "SELECT rssID,title,description FROM rsstest WHERE 1 ORDER BY date desc limit 10";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
?>
<item>
<title><?= htmlentities($row["title"]); ?></title>
<description><?= htmlentities($row['body']); ?></description>
<link>http://www.yourdomain.com/news.php?id=<?= $row['rssID']; ?></link>
</item>
<?
}
?>
</channel>
</rss>
Explanation:
- The first line is just a database include I created. Replace this with your include file or MySQL connection code
- Next we are beginning to build the XML header and structure that the RSS specification requires.
- All RSS data is enclosed between start & end brackets similar to HTML. The "title", "description" and "link" data are basic information about your website.
- Next, a SQL call is made to grab the content from the database that we want in the RSS feed.
- A while loop takes care of writing each line of RSS data & start/end brackets to each XML entity.
That's it! That's all there is too it. A dynamic RSS feed is simply a ".rss" file that is given PHP functionality by Apache using .htaccess. The only tricky part of it all is having the PHP code write nice XML code. You can then link "rsstest.rss" on your website & users would be able to access the most up-to-date information in your feed.
Comments:
Re: How do I get this to read from two or more tables? |
Posted 09/21/09 7:41PM by AceBHound |
Yes it's possible to use two different tables to create the RSS feed. A simple PHP method would be to use PHP arrays and load the table data to the arrays, then loop through the arrays to output the XML. If you wanted to combine both sets of data and order by date, title, etc it might be easier to create a new "staging" table and load the data from both tables into it. You might be able to do that through a temporary table via a SQL query. You could also have a CRON job run every few hours to purge a physical staging table and insert rows from the other two tables. Then have your RSS feed use the staging table data. |
How do I get this to read from two or more tables? |
Posted 05/06/08 9:41AM by jonthomas83 |
Hi, excellent stuff, I’ve been wanting to do this for a while, thank you for your code. I’m in the process of designing a complex database driven website and was wondering if there is a way to get data to be published in the RSS feed from a "latest news" table and also from a "weekly tv listings" and possibly even a third table. The idea being that when news is updated on the site, the RSS shows the update, but also the RSS would hopefully show the latest TV listings for programs in the tv listings table as and when they are updated. There are different fields in each table and the structure of each is different. Can this be achieved? The field names are different in each table and there are more fields in one than the other so I’m assuming there’d be some kind of PHP statement in there that gets the latest posts from either of the tables? Please help! :) Many thanks Jonathan |
Re. Creating Dynamic RSS Feeds With PHP |
Posted 03/23/08 11:31AM by AceBHound |
Database connection would look something like this: $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_database"); |
Creating Dynamic RSS Feeds With PHP |
Posted 03/23/08 7:07AM by Anonymous Techdoser |
can you please write how should databse connection look like? Thanks |
it works thanks great |
Posted 05/25/06 11:41AM by Anonymous Techdoser |
i have implemented this on centraldirectory.net/feeds.rss this rocks thank you very much |