How to: Dynamic RSS feed using PHP/MySQL

Introduction:

After just implementing my first RSS feed I thought I would share how I did it as there was not a lot of info on it. RSS feedphotos allows readers to see that updates have been made to a site without having to visit the site itself. This guide will assume that you have previous knowledge of PHP, MySQL and XML.

Requirements:

  • Webserver with:
    • PHP
    • MySQL

Basics:

We will start with just a basic static RSS feed to give you and idea of what a basic feed looks like.

<?xml version="1.0"?>
<RSS version="2.0">
<CHANNEL>
<TITLE>Your site name here</TITLE>
<DESCRIPTION>A description of your site</DESCRIPTION>
<LINK>http://www.yourtotalyawesomewebsite.com</LINK>
<ITEM>
<TITLE>Update title</TITLE>
<DESCRIPTION>Your update or body of your update</DESCRIPTION>
<LINK>http://www.yourtotalyawesomewebsite.com/update.php</LINK>
</ITEM>
</CHANNEL>
</RSS>


This is your basic RSS feed you will notice that it is an XML document, this is very impirtant.

Making it dynamic:

Now that we have a basic feed we can modify it to make it dynamic. First we will make a PHP file, I called mine rss.php. The first and very important thing we have to do is to make our PHP file look like an XML page, to do this we will use the PHP header function like so:

<?php header('Content-type: application/xml'); ?>

This line has to be at the very top of your page and the first thing, now to the outside world your PHP file looks like an XML file. Now that we have that line at the top of your page copy and paste the basic feed below it. Once we have that done we can now start adding our dynamic content. This is jst like making dynamic content as you normally would with PHP/MySQL.

<?php
//conection configuration stuff
include ('config.php');
include ('opendb.php');
$news = mysql_query("SELECT news, header FROM news ORDER BY article DESC LIMIT 3", $conn); //query to get 3 latest updates and headers
//while loop to echo out the results of the query
while ($row = mysql_fetch_array($news)) {
?>


Now we just have to echo out the informatin into the correct tags, we will want to do this for the title, description, and link. For me my link was all back to the same page so mine is hard coded but you would put it in just like you would for title and description. The following is the code to echo the info from the database into the feed.

<item>
<title><?=htmlentities(strip_tags($row[1]))?></title>
<link>http://www.bryntassell.ca/photo</link>
<description><?=htmlentities(strip_tags($row[0]))?></description>
</item>
<?php
}?>

You will see =htmlentities(strip_tags($row[0])), this is very important as it strips out any HTML tags that which will make you feed not validate, more on that later. Thats all of the little pieces so here it is all put together.

<?php
header('Content-type: application/xml');
?>
<rss version="2.0">
<channel>

<title>Photography by Bryn Tassell</title>
<link>http://www.bryntassell.ca/photo</link>
<description>A collection of photography by Bryn Tassell</description>
<language>en-us</language>
<?php
//conection configuration stuff
include ('config.php');
include ('opendb.php');
$news = mysql_query("SELECT news, header FROM news ORDER BY article DESC LIMIT 3", $conn); //query to get 3 latest updates and headers
//while loop to echo out the results of the query
while ($row = mysql_fetch_array($news)) {
?>
<item>
<title><?=htmlentities(strip_tags($row[1]))?></title>
<link>http://www.bryntassell.ca/photo</link>

<description><?=htmlentities(strip_tags($row[0]))?></description>
</item>
<?php
}
?>
</channel>
</rss>

Conclusion:

That is our complete dynamic RSS feed. Now all that is left to do is to validate your feed to make sure it will display properly. If you have HTML tags in it seems to work but not in all readers, so it is best to strip them out. Now add a link to your page and give your feed a try!

Links:

Here are some handy links that you may find useful.

www.feedburner.com – A free service that gives you some handy features to better manage your feed.

http://feedvalidator.org – Use this to validate your feed.

http://www.feedicons.com
– Get some nice icons to advertise your new feed.

Leave a Reply

Your email address will not be published. Required fields are marked *