I’m lacking in work samples so I spent the last few days of my vacation coding. Since I enjoy blogging, I decided to create a light-weight and very simplistic blogging platform. It’s called Slashblog and you can look at the code so far here.
Every blog needs an RSS feed. The fact that I have no idea of how to create one makes it a pleasant task. Hopefully, it’ll also be an educational experience that I can share with you.
The RSS 2.0 specification was released in 2003 so there is plenty of information around. What I already know is that RSS feeds are basically XML. Since my application already stores posts as XML, my initial thought was; why not make my XML fit the RSS format?
http://www.linktosomeblog.com/
This is the description of some kind of blog, probably.
-
http://www.linktosomeblog.com/thepost
This could be an excerpt from the blog post or it could be the entire content.
Tue, 29 April 2008 08:56:02 GMT
A simple RSS 2.0 structure. The title, link and description elements of channel are required. We have a fixed set of elements to work with in item, none of which are actually required but these are the ones I’ll be using. Did I decide to use this format to store my posts?
No. The fact is that it would be a blow to the scalability of my application. RSS can be extended to support elements outside its specification but only with the use of namespaces. Besides from XML, the application can also store data in flatfiles and I’ll probably add support for MySQL.
Using namespaces would be messy as the XML element names would differ from any SQL fields names. This can be fixed but it would require more code to process. We would also be responsible for keeping the XML file constantly up to date, every time we publish, delete or edit a post. It makes much more sense to have a function print the posts as RSS on demand.
I ended up with a method of the class Blog called PrintRSS(). My code may look a bit odd to you since it’s object-oriented but hopefully it still makes sense.
public function PrintRSS()
{
if(!$this->PostsLoaded()) // If no posts are loaded...
$this->LoadPosts(); // ...then load them now.
// Compose all sections of the RSS data.
$header = '' . PHP_EOL . '' . PHP_EOL . '' . PHP_EOL;
$header .= '' . PHP_EOL;
$header .= '' . $this->GetSetting("DESCRIPTION") . ' ' . PHP_EOL;
$header .= '
' . $this->GetSetting("URL") . '' . PHP_EOL;
$items = "";
foreach($this->posts as $post)
{
$items .= '- ' . PHP_EOL;
$items .= '' . PHP_EOL;
$items .= '
' . $post->GetContent() . ' ' . PHP_EOL;
$items .= '
' . $post->GetDate() . ' ' . PHP_EOL;
$items .= '
' . $this->GetSetting("URL") . 'index.php?post=' . $post->GetID() . '' . PHP_EOL;
$items .= ' ' . PHP_EOL;
}
$footer = ' ' . PHP_EOL . ' ';
echo $header . $items . $footer; // Write it all to file.
}
Now we just need a file that loads all of my posts and actually calls the method to display the RSS. Short and sweet, as follows.
LoadPosts();
$blog->PrintRSS();
?>
That’s it, works perfectly. What we have is a dynamically created, RSS formatted XML file. It’s available at dev.slashblogger.com/rss.php for now and I’ve tried it out in Google Reader. To be honest, I expected it to be more advanced than this. But the job is done.
Resources
PHP and RSS: Getting it together
RSS at Wikipedia
RSS 2.0 Specification
Thanks for your valuable artile.….
thanks a lot