Many sites offer you the chance to subscribe to their site through the use of a web feed, namely RSS ( Really Simple Syndication), for quick and easy automatic updates to the sites you love. If you're a web developer who updates his site frequently and wants to offer the same service to your subscribers, you'll find that it's actually quite easy to create.
RSS : The Need-To-Know
When you check a RSS feed, you see lots of information: information about the site you're receiving the feed from, and more importantly, the blog posts which have been posted on the site. Keep all of this in mind: you'll need it when you create your own custom RSS feed.
RSS has a strict format which you need to follow in order for it to validate and work with all different RSS programs. You can use
RSS Validators to check what you have coded is valid. It has a XML structure, detailing all of the different parts of the feed and is case sensitive! This is very important.
Creating your RSS Feed
I'm going to assume you already have a MySQL table setup containing all of the information you want to display in your RSS feed. It should include information such as Titles, Descriptions and Dates.
Create a new file called rss.php. We'll be tackling the problem by building our RSS feed into the variable
$build, and then echoing it out into a file later.
Our first step is to define the content type as XML and that its contents will be a RSS feed:
$build = '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
We also open the
<channel> tag. This surrounds all of our RSS content.
Next, we fill in details about the site the feed is coming from. Here is the information I am using:
<title>Blog Posts from AnujNair.com</title>
<link>http://anujnair.com/</link>
<description>The most recent blog posts from AnujNair.com</description>
<language>en-us</language>
<lastBuildDate>'.date('D, d F Y H:i:s O').'</lastBuildDate>
<managingEditor>admin@anujnair.com (Anuj Nair)</managingEditor>
<webMaster>admin@anujnair.com (Anuj Nair)</webMaster>
<copyright>Anuj Nair</copyright>'."\r\n";
Obviously, replace it with your own information. title, link, description, language and lastBuildDate are all recommended information to include. The rest are optional.
managingEditor and webMaster should be of the format
Email (Full Name). This is for validation.
Now that our website information has been built, we need to build the individual blog posts information. Individual blog posts are displayed between the
<item>, like so:
<item>
<title></title>
<guid></guid>
<link></link>
<description></description>
<pubDate></pubDate>
<author></author>
</item>
We'll use a MySQL call to fetch all of our data, and then loop through each and build the RSS feed items:
$result = mysql_query("SELECT * FROM `blog` ORDER BY `ID` DESC LIMIT 10");
while ($row = mysql_fetch_assoc($result)) {
$build .= "\t\t\t".'<item>
<title>'.htmlentities($row['Title']).'</title>
<guid isPermaLink="true">http://anujnair.com/blog.php/'.$row['ID'].'/'.$row['Title'].'</guid>
<link>http://anujnair.com/blog.php/'.$row['ID'].'/'.$row['Title'].'</link>
<description>'.htmlentities($row['Description']).'</description>
<pubDate>'.date('D, d M Y H:i:s O', strtotime($row['Date'])).'</pubDate>
<author>'.$row['Email'].' ('.$row['Author'].')</author>
</item>'."\r\n";
}
Make sure you use
htmlentities to escape all characters in your title and description. Your feed won't validate if HTML entities aren't escaped properly.
pubDate is also very picky. The Date format
must be as shown above, otherwise your RSS feed will not validate.
isPermaLink tells the RSS feed that the link to the blog post is permanent.
Finally, we close the channel and the RSS feed:
$build .= "\t\t".'</channel>
</rss>';
Now we have everything in our
$build variable, we're going to output it to a rss.xml file. This way, we'll be loading data from a static file everytime someone requests our RSS feed, and not from the database. If 1000's of people subscribe to our feed, and we fetch data from our database each time, we'll probably end up crippling our site! Instead, we'll choose to update a static file every day via a cron job. The final script looks like this:
<?php
include("mysql.php");
$fd = fopen("rss.xml", "wb");
$build = '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>Blog Posts from AnujNair.com</title>
<link>http://anujnair.com/</link>
<description>The most recent blog posts from AnujNair.com</description>
<language>en-us</language>
<lastBuildDate>'.date('D, d F Y H:i:s O').'</lastBuildDate>
<managingEditor>admin@anujnair.com (Anuj Nair)</managingEditor>
<webMaster>admin@anujnair.com (Anuj Nair)</webMaster>
<copyright>Anuj Nair</copyright>'."\r\n";
$result = mysql_query("SELECT * FROM `blog` ORDER BY `ID` DESC LIMIT 10");
while ($row = mysql_fetch_assoc($result)) {
$build .= "\t\t\t".'<item>
<title>'.htmlentities($row['Title']).'</title>
<guid isPermaLink="true">http://anujnair.com/blog.php/'.$row['ID'].'/'.$row['Title'].'</guid>
<link>http://anujnair.com/blog.php/'.$row['ID'].'/'.$row['Title'].'</link>
<description>'.htmlentities($row['Description']).'</description>
<pubDate>'.date('D, d M Y H:i:s O', strtotime($row['Date'])).'</pubDate>
<author>admin@anujnair.com (Anuj Nair)</author>
</item>'."\r\n";
}
$build .= "\t\t".'</channel>
</rss>';
fwrite($fd, $build);
fclose($fd);
?>
Now link rss.xml on your site and setup your cron job to run this script once or twice a day.
Job done!