Sunday, April 5, 2009

PHP and MySQL RSS feed

If you have a database driven site with regularly updated contents then it is a good idea to setup a RSS feed for your site. RSS (Really Simple Syndication) can be seen on almost every blog and websites this days.

In this tutorial, ill assume that you already know PHP and MySQL. First, youll need PHP to declare an XML document.


header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";


The "header" sets the HTTP header into text/XML so the rss reader knows that the content is XML.

Next step is, to declare the RSS version


echo "
<rss version=\"2.0\">";


Then, we create the actual XML document.


echo "
<channel>
<title>Feed title</title>
<description>A description of the feed contents</description>
<link>http://www.yoursite.com/</link> ";


What the code does is create the opening channel,title,etc.

Then, second, connect the PHP page into your MySQL.


@mysql_connect("server" , "username" , "password");
@mysql_select_db("database_name");


Then, pull data into your database. Using the function mysql_fetch_array.


$data = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 10");
while($row = mysql_fetch_array($data))
{
$row[Intro] = str_replace("images/", "http://www.yoursite.com/images/",$row[Intro]);


What the str_replace function is very important if the columns that you are getting data from have links to images.

Next part is where the xml is filled with data.


echo "
<item>
<link>http://www.yoursite.com/article.php?id=".$row[id]."</link>
<guid isPermaLink=\"true\">http://www.yoursite.com/article.
php?id=".$row[id]."</guid>
<title>".$row[Title]."</title>
<description><![CDATA[".$row[Intro]."]]></description>
<comments>http://www.yoursite.com/article.php?id=".$row[id]."#Comments</
comments>
</item>";
}


The important of this data is the CDATA. If you want the full document of what are the XML tags available for RSS is

http://www.feedvalidator.org/docs/

Heres the complete source code..


<?php
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
// Set RSS version.
echo "
<rss version=\"2.0\"> ";
// Start the XML.
echo "
<channel>
<title>Feed title</title>
<description>A description of the feed contents</description>
<link>http://www.yoursite.com/</link>";
// Create a connection to your database.
@mysql_connect("server" , "username" , "password");
@mysql_select_db("database_name");
// Query database and select the last 10 entries.
$data = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT 10");
while($row = mysql_fetch_array($data))
{
// Convert database images data into actual image link.
$row[Intro] = str_replace("images/", "http://www.yoursite.com/images/", $row[Intro]);
// Continue with the 10 items to be included in the <item> section of the XML.
echo "
<item>
<link>http://www.yoursite.com/article.php?id=".$row[id]."</link>
<guid isPermaLink=\"true\">http://www.yoursite.com/article.php?id=".$row[id]."</guid>
<title>".$row[Title]."</title>
<description><![CDATA[".$row[Intro]."]]></description>
<comments>http://www.yoursite.com/article.php?id=".$row[id]."#Comments</comments>
</item>";
}
echo "
</channel>
</rss>";
?>


That's it.

1 comment:

  1. If you want to do the opposite, aggregate material from rss feeds into mysql, cheap script here http://www.rss2mysql-feed-randomizer-compiler.com/

    -- hey btw nice code display, id like to see a tutorial on how to incorporate this into blogspot.

    ReplyDelete