Topics: 15 | Posts: 14 | Comments: 12 | Members: 170
RSS Feeds for your website or content are very important in this day and time. Various organizational customers and online users stay updates using RSS readers which allow them to subscribe to the RSS feeds from those sites. In addition, RSS Feeds are another method of promoting your content through social media. Basically, an RSS Feed is an XML file (as will be discussed) enabling it to be identified by RSS Readers as a validated Feed. Let’s have an inside look into the basic structure of an RSS Feed. You can utilize any text editor for creating your documents (Notepad++ a a great and free example of such editors). These feeds basically consist of four main components (HEADER, DOCUMENTATION, CONTENT and FOOTER) in which our content would split up into sub sections. HEADER: This is more or less the declaration of the document and its components. Since we will be using PHP to create our feed, there are additional components that we need to include in our markup.
<?php
// basic server reconciliation
ob_start();
?>
<?php
// tells the server that our document output is in RSS/XML format
header(\"Content-Type: application/rss+xml; utf-8\");
?>
<?php
// declares our XML document and RSS version
echo \'<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">
<channel>\';
// add a line break for better code structure
echo\"\\r\\n\";
?>
Now that we have our header content, we move on to our DOCUMENTATION providing additional declarations.
DOCUMENTATION:
Which helps define our Feed and assists RSS reader in discovering your feed and provide additional information to your subscribers.
<?php
// title of your feed
echo \'<title>domain.tld News and Updates</title>\';
echo\"\\r\\n\";
// link to your site or blo
echo \'<link>http://www.domain.tld/</link>\';
echo\"\\r\\n\";
// description of your feed
echo \'<description>The latest technology news and updates domain.tld.</description>\';
echo\"\\r\\n\";
// language structure of feed
echo \'<language>en-us</language>\';
echo\"\\r\\n\";
// date at which the feed was fully published
echo \'<pubDate>Thu, 22 Mar 2012 15:30:00 GMT</pubDate>\';
echo\"\\r\\n\";
// last date in which the feed was updated
echo \'<lastBuildDate>Thu, 22 Mar 2012 15:30:00 GMT</lastBuildDate>\';
echo\"\\r\\n\";
// official RSS documentation (important)
echo \'<docs>http://blogs.law.harvard.edu/tech/rss</docs>\';
echo\"\\r\\n\";
// link at which the feed was generated (optional)
echo \'<generator>yourdomain.tld</generator>\';
echo\"\\r\\n\";
// email and full name of support team in case or server errors.
echo \'<managingEditor>[email protected] (Jain Doe)</managingEditor>\';
echo\"\\r\\n\";
// email and full name of webmaster in case or server errors.
echo \'<webMaster>[email protected] (John Doe)</webMaster>\';
echo\"\\r\\n\";
// link to your feed url (important)
echo \'<atom:link href=\"http://yourdomain.tld/yourfeed\" rel=\"self\" type=\"application/rss+xml\" />\';
echo\"\\r\\n\";
?>
Now that we have organized the basic structure of our feed, all we need to do is add our content which we will be gathering from our database.
Inclusive of this, there are specific operations that we need for our RSS operators to be valid like the date is has to be displayed in a specific format.
CONTENT:
This is where we list the contents of our news/updates in the format of an RSS Feed.
<item>
<title>Title of Article</title>
<!-- Link to official article -->
<link>http://www.yourdomain.tld/blog/article</link>
<!-- unique identifier in which since link is different, the link to the article can be used -->
<guid>http://www.yourdomain.tld/blog/article</guid>
<!-- Published date of the article -->
<pubDate>Mon, 02 June 2012 12:13:29 GMT</pubDate>
<description>Partial description of the article</description>
</item>
Above is the basic structure of an RSS Feed entry which we will be using to display each article from our database.
<?php
// this function allows to limit the amount of words we will be outputting from our description (optional)
function limit_words($string, $word_limit)
{
$words = explode(\" \",$string);
return implode(\" \",array_splice($words,0,$word_limit));
}
// connect to our database
$dbhost=\"localhost\"; // server host
$dbname=\"mydb\"; // database name
$dbuser=\"user\"; // database username
$dbpasswd=\"password\"; // database password
// connect to the db
$link = mysql_connect($dbhost, $dbuser, $dbpasswd);
if (!$link) {
die(\'Could not connect: \' . mysql_error());
}
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
die (\'Can\\\'t use dbreviews : \' . mysql_error());
}
// Get all articles
$blog_data = mysql_query(\"SELECT *, DATE_FORMAT(date, \'%a, %d %b %Y %T\') AS rssPubDate, FROM articles ORDER BY date DESC LIMIT 10\"); // change \"articles\" to your table name and either remove or change \"LIMIT 10\" to limit the number of entries
// loop through each entry
while ($article = mysql_fetch_array($blog_data))
{
// this will get the date of our article in RSS format
$rssPubDate = $article[\'rssPubDate\'];
$timeZone = date(\'T\');
$rssPubDate .= \" {$timeZone}\";
// article description
$description = getVar(str_replace(\"<br />\",\"\",limit_words($article[\'description\'],35)));
//strip all tags to avoid errors
$description = strip_tags(getVar(str_replace(\"<br />\",\"\",limit_words($article[\'description\'],35))));
?>
<!-- Insert information into RSS Feed Format -->
<item>
<title>
<?php echo $article[\'title\']; ?></title>
<link>http://appspyre.com/blog/articles/<?php echo str_replace(\" \",\"_\",$article[\'title\']); ?></link>
<guid>http://appspyre.com/blog/articles/<?php echo str_replace(\" \",\"_\",$article[\'title\']); ?></guid>
<pubDate>
<?php echo $rssPubDate; ?></pubDate>
<description>
<?php echo $description; ?></description>
</item>
<?php }
?>
The above code will fully organize and display our RSS data into out official feed. And embedding the information into item tags will display each entry as entered.
FOOTER:
Finally, we close off our RSS Feed in finalizing all information.
</channel>
</rss>
Conclusion:
Now that we have created our RSS Feed, you can display it on your site or use an RSS reader like Feed Burner to capture and display your articles in a web and user friendly format.
Additionally, to ensure that your users browser detects the RSS feed on your site, insert the following code between the tags of your web pages.
<link rel=\"alternate\" href=\"/feed\" title=\"My RSS Feed\" type=\"application/rss+xml\" />
Ensure to replace /feed in the href tag with the url to your feed.
reply