Topics: 15 | Posts: 13 | Comments: 12 | Members: 167
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";
?>
<?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";
?>
<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>
<?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 }
?>
</channel>
</rss>
<link rel="alternate" href="/feed" title="My RSS Feed" type="application/rss+xml" />
Interesting. I've had trouble with RSS feeds in the past. I'll have to try this sometime.