ArtEnthu has recently launched a RSS feed to all the content posted at ArtEnthu.com. Thanks to an Art Enthusiast working for a SEO Group, now anybody can link to this feed and keep tab of the latest on ArtEnthu.com.
This article would be useful for anybody looking to make a dynamic RSS feed for his website in the LAMP stack (PHP implementation).
RSS mean “really simple syndication” and one can check out the details http://www.rssboard.org/rss-specification.
Let’s start with answering the very basic question about enabling RSS feed to website content. Why do I need RSS on my website?
· First and foremost, it is one of the least cumbersome way for your viewer to check the content from the site without actually visiting it.
· Interesting content would often translate to more page views of the website. A very well distributed RSS feed means that content would seldom go unnoticed.
· You can link your website RSS to social networking site. This reduces your efforts to manually maintain each of your social profiles.
· It’s a best practise and helps improve Google ranking for the page. Talk to your SEO for explanation on this one J
Once you are convinced that you are up for the challenge to do the RSS feed implementation for your website, there are many ways to do this:-
· Maintain XML file which confers to the RSS specification manually. And whenever you want to update the feed, update this file manually.
· Use the third party or open sources software to provide simple integration in your website to build this XML file for your. Efforts are less but you need to do the installation on your server and configure this for your website.
· Write your own code to build the XML format. I recommend this approach and that the main reason I did not provide any link or details on the above two approaches.
Approach to make the RSS feed for your website:-
Let us assume you have a content based site, likes news or articles or entertainment content that gets updated in your website. Another example could be you have image based website, showing various images and less content.
In both the cases you will be having the database storing these contents or images with timestamp. In case you do not maintain the timestamp, start having one which keeps track of creation of content or image on the server.
Once you have the timestamp of your data, you can start process of exposing a RSS feed. Simple steps below outline the entire process:-
- Connect to the database
- Create RSS channel
- Create the items with latest timestamp as first with simple query to your content or image with alt text using the “order by date” sql. And offcourse with “limit 20” or any number you want to publish in your RSS feed.
This would mean fetch the latest 20 records of your update as RSS at any point users try to get the feed.
Above approach is easy and completely dynamic with no maintenance at all. Once you put the code in a PHP file you are done with the implementation for the RSS.
The code below would produce an XML as per the RSS specifications. This xml can be very by the RSS validator (http://beta.feedvalidator.org/) and read by an RSS reader (popular browsers like IE or mozilla or CHROME have reading capabilities).
PHP Implementation:-
We consider the news website!
<?php
/** Get the database connection */
$con = mysql_connect(”serverName”, “UserName”, “Password”);
if (!$con)
{
echo ” not connection”.mysql_error();
die(’Could not connect: ‘ . mysql_error());
}
mysql_select_db(”databaseName”, $con);
/** RSS Dynamic Feed Code */
/** Define the channel */
$channel = array(”title” => “Genuine News of world!”,
“description” => “News you cannot get anywhere”,
“images” => “http://www.yourwensite.com/images/logo.png”,
“altText” => “genuine news”,
“link” => “http://www.yourwebsite.com”,
“copyright” => “Copyright (C) 2010 yourwebsite.com”);
$newOutput = ‘<?xml version=”1.0″ encoding=”ISO-8859-1″?>’;
$newOutput .= ‘<rss version=”2.0″>’;
$newOutput .= “<channel>”;
$newOutput .= “<title>”.$channel["title"].”</title>”;
$newOutput .= “<description>”.$channel["description"].”</description>”;
$newOutput .= “<link>”.$channel["link"].”</link>”;
$newOutput .= “<category>Business News</category>”;
$newOutput .= “<copyright>”.$channel["copyright"].”</copyright>”;
/** get the latest post on the website… */
$sql = “select newsLinkPage, newsTitle, newsShortContent, imgPath, newscreationDate from newsTable where order by newscreationDate desc limit 20″;
$qry = mysql_query($sql, $con);
if ( $qry )
{
while ( ($row = mysql_fetch_array($qry, MYSQL_ASSOC)) )
{
$output .= “<item>”;
$output .= “<title>”. htmlentities ($row['newsTitle']).”</title>”;
$imgdata = “<p>”.$row[‘newsShortContent’].”</p><br clear=’all’ />”;
$output .= “<link>”. $row[‘newsLinkPage’].”</link>”;
$output .= “<pubDate>”.date(”D, d M Y H:i:s O”, strtotime($row[' newscreationDate '])).”</pubDate>”;
$preDesc = “<p><A href=’”.$row[‘imgPath’].”’><img src=’$imgPath’ alt=’”.htmlentities ($row['newsTitle']).”‘ align=’left’ border=0 /></A>”;
$desc = $preDesc.$row['newsShortContent'].”</p><br clear=’all’ />”;
$output .= “<description>”.htmlentities($desc).”</description>”;
$output .= “<category>business news</category>”;
$output .= “</item>”;
}
}
$output .= “</channel>”;
$output .= “</rss>”;
header(”Content-Type: application/rss+xml; charset=ISO-8859-1″);
echo $output;
?>
It’s that simple, just copy the above code and change it accordingly to suit your need.
Last few things you need to take care before it’s integrated with your website.
- Link this in your website using the standard RSS feed Icon
<A href=’rss.php’><img src=’rssIcon.png’ border=0/></A>
- Add the tag in the page where RSS link is available so that the browser recognizes the RSS feed. Embed the following code inside the head tag, just below the meta tag:
<link rel=”alternate” type=”application/rss+xml” title=”Yourwebsite Title” href=”http://www.yourwebsite.com/rss.php” />
Check out the RSS feed of artenthu at http://artenthu.com/rss.php
In the above example rss.php is assumed to be placed in root directory. In case you create the sub directory change the links accordingly. So go RSS savvy today!
Feel free to provide your feedback.
Team ArtEnthu.