Create an RSS feed from MySQL data with PHP
In this tutorial I will show you how I use information already stored in my database to create an RSS feed
We are going to use PHP to convert the data into rss so first off we need to tell the server to parse any .rss files as PHP.
You can do this using the Apache AddType directive.
Add the following line to your .htaccess file (or create a file called .htaccess and upload it to your root directory:
AddType application/x-httpd.php .rss
Some servers might require the following line instead (note the replacement of '.' with '-') :
AddType application/x-httpd-php .rss
Furthermore some servers my require the additional line:
AddHandler x-httpd-php .rss
Next we will look at the code and then we will proceed to break it down step by step
Firstly we need to set a 'content-type' header so that the end users aggregator or browser knows that the file is rss/xml.
RSS is an XML based language and as such we need to define it.
Next we connect to the database. I'm not going to go in to how to connect to a database in this tutorial, all I will say is that I use a seperate file for this purpose and that the following line 'includes' the script.
Similarly to the html tag in html all rss files are enclosed by rss tags.
Next is the 'channel' tag. this also surrounds the rest of the document content.
Before we start echoing out each item in the db we need to set a title, description and link for the document
Now we simply select the the table in the database we want to use and then run through each entry echoing the required fields as we go. Please note that there a quite a few different available item elements in the RSS2 specs, for the purpose of this tutorial we are only going to use 4 of the most commonly used ones.
Notice that each item is surrounded by <item> tags and then each element of that item has its own tags
You should also take into consideration the type of data you are extracting from the database and filter it accordingly, for example if your description data contains html tags you would need to use htmlspecialchars, strip_tags or the like.
Comments:
Featured Script
PHP Send this page to a friend script
as the title suggests you can add this script to any of your pages and and it will allow your visito... (read more)
