RSS is a great way to syndicate content of your site. RSS readers are becoming more and more popular. Also community web site such as technorati and ping-o-matic make it great to share and post your content/blogs. RSS is the future, so it's best to get on it.
This how-to will explain how to create a RSS/XML feed using PHP and cron to do a daily XML dump of content stored in a database.
The first step is to create a directory above the web root. Let's name it /cron. In this directory we'll create a PHP-CLI script or command-line script. This script will be executed by cron.daily, so every night at 3AM this script will run. This will not need any interaction with a web browser, but you'll need the ability to create an executable PHP script on the web server and have access to your crontab.
Next let's create the file rss.php in our newly created /cron directory. I'll break it down and explain it as we go through the script. After creating the file we need to assign the interpreter to process the script.
#!/usr/bin/php
<?
Set up database credentials and establish the connection.
$DBserver = "localhost";
$DBname = "yourDB";
$DBuser = "username";
$DBpassword = "password";
$webroot = "http://www.yourdomain.com/";
$rootpath = "/var/www/www.yourdomain.com/htdocs/";
$docRoot = $rootpath;
if(!($connect = mysql_connect($DBserver, $DBuser, $DBpassword))){
echo "Error Connecting to the Database.";
exit();
}
$DB = mysql_select_db($DBname);
Query the database and pull blog entries. This example is pulling the last 10 active entries from the database.
$sql = "SELECT BlogDate, BlogTitle, BlogBody, ID
FROM tblBlog
WHERE Active = 1
ORDER BY BlogDate DESC
LIMIT 10";
if(!$rs = mysql_query($sql,$connect)){
echo "Database Errorn";
echo mysql_error()."\n".$sql."\n";
exit();
}
Now, we'll assign a local variable and begin assembling our RSS/XML string
$rssString = "";
$rssString .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$rssString .= "<rss version=\"2.0\">\n";
$rssString .= "<channel>\n";
$rssString .= "<title>YourDomain.com - RSS Feed</title>\n";
$rssString .= "<link>http://www.yourdomain.com/</link>\n";
$rssString .= "<description>YourDomain.com</description>\n";
$rssString .= "<lastBuildDate>".date("D, j M Y H:i:s T")."</lastBuildDate>\n";
$rssString .= "<language>en-us</language>\n";
Loop through your recordset and add title, date, body, etc.
while($row = mysql_fetch_object($rs)){
$BlogDate = $row->BlogDate;
$BlogTitle = htmlspecialchars(strip_tags($row->BlogTitle));
$BlogBody = substr(htmlspecialchars(strip_tags($row->BlogBody)),0,200)."...
";
$ID = $row->ID;
$rssString .= "<item>\n";
$rssString .= "\t <title>".$BlogTitle."</title>\n";
$rssString .= "\t <link>".$webroot."blog.php?ID=".$ID."</link>\n";
$rssString .= "\t <guid>".$webroot."blog.php?ID=".$ID."</guid>\n";
$rssString .= "\t <description>".$BlogDate." - ".$BlogBody."</description>\n";
$rssString .= "</item>\n";
}
mysql_free_result($rs);
$rssString .= "</channel>\n";
$rssString .= "</rss>\n";
Use fopen() and fwrite() to create the XML file. Make sure you close the object when you're done.
if(!$fp = fopen($rootpath."rss.xml",'w')){
echo "Could not open the rss.xml file";
exit();
}
if(!fwrite($fp,$rssString)){
echo "Could not write to rss.xml file";
exit();
}
fclose($fp);
?>
Test this by using your terminal (assuming that you're SSH'd into the web server). You can test this by issuing the following command...
./path/to/cron/rss.php
If you don't see any error messages and the XML is written out, you were successful. All that is left to do is to add this in /etc/cron.daily and you'll have daily rss updates. If you need to update it more than that you can create a specific time/frequency in your crontab (ie: every 15 minutes, etc). If you do not have access to command line, then you'll need to create this script under the web root and set lynx (text browser) to execute the script on a schedule.
Good luck!
Go Back
|
Feed Validation JimBo | 11/05/06 at 7:57PM You can use this to validate your RSS feed. http://validator.w3.org/feed/check.cgi |
|
Great admin | 11/07/06 at 08:34AM Thanks for posting the RSS feed validator. Good link to have. admin |
|
Most of the world thinks Spamming sucks AntiSpam | 02/14/07 at 11:14AM I think DevSnips.com is awesome. Talk about a great repository of code snips and useful resources. Thanks!!! |
|
You are right Juarezz | 02/22/07 at 5:49PM Greetings! Author, I almost agree with you. And glamourous journal, interesting site name www.devsnips.com :), I see you you're are not newbe. Keep up with the great work! |
|
Great subject MotorXX | 02/22/07 at 10:34PM Hi there! Author, you are so right! And cool blog, interesting site name www.devsnips.com :), I see you you're are not newbe. Don't stop the nice work! |
