How To Create An RSS 2.0 Feed

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?

<?xml version="1.0"?>
<rss version="2.0">
    <channel>
        <title>Blog Title</title>
        <link>http://www.linktosomeblog.com/</link>
        <description>This is the description of some kind of blog, probably.</description>
        <item>
            <title>A Post Title</title>
            <link>http://www.linktosomeblog.com/thepost</link>
            <description>This could be an excerpt from the blog post or it could be the entire content.</description>
            <pubDate>Tue, 29 April 2008 08:56:02 GMT</pubDate>
    </item>
    </channel>
</rss>

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 = '<?xml version="1.0"?>' . PHP_EOL . '<rss version="2.0">' . PHP_EOL . '<channel>' . PHP_EOL;
    $header .= '<title>' . $this->GetSetting("TITLE") . '</title>' . PHP_EOL;
    $header .= '<description>' . $this->GetSetting("DESCRIPTION") . '</description>' . PHP_EOL;
    $header .= '<link>' . $this->GetSetting("URL") . '</link>' . PHP_EOL;
    $items = "";
    foreach($this->posts as $post)
    {
        $items .= '<item>' . PHP_EOL;
        $items .= '<title>' . $post->GetTitle() . '</title>' . PHP_EOL;
        $items .= '<description>' . $post->GetContent() . '</description>' . PHP_EOL;
        $items .= '<pubDate>' . $post->GetDate() . '</pubDate>' . PHP_EOL;
        $items .= '<link>' . $this->GetSetting("URL") . 'index.php?post=' . $post->GetID() . '</link>' . PHP_EOL;
        $items .= '</item>' . PHP_EOL;
    }
    $footer = '</channel>' . PHP_EOL . '</rss>';
 
    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.

<?php
    require_once 'config.php';
 
    $blog = new Blog();
 
    $blog->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

Long Time No C

I’ve decided to move away from this god-forsaken place and into the sinful capital of Europe, Amsterdam. Or Hamsterdam as my sister likes to call it. Anyway, obviously this means that I’m now looking for work. This also leads me to wonder in which industry and profession I really belong.

We all wonder this sometimes but I don’t think there are many people that find it as uncomfortable as I do. The thought of not knowing where I belong and what my purpose is, it bothers me. Makes me think that whatever I do is a waste of time.

I mean, it’s like wandering around without knowing where to go - pretty pointless, don’t you think? And I’m a person that just can’t stand still. Always need to progress, develop, grow, succeed. Patience, bah.

So what do I do now and why not continue doing that? Right now I’m technical support for some big players in the IT-retail industry. I know Windows XP and Vista inside and out, and I can make pissed off people smile. I can practically do the job in my sleep, so why change?

Don’t get me wrong here. It’s an okay job and the pay is fine but you know what? It’s just not me. On the contrary to what you may think (and what I thought before I started,) technical support is not equal to problem solving. Sure, we fix things. Lots of things, actually. But the thing is; at the end of the day it’s all just repetitive work in an all too controlled environment.

So what’s my profession? I realize now that the answer is in my past and in my backbone. It’s been obvious for so long, yet I haven’t done anything about it. Really, I’m disappointed in myself.

You see, ever since I was a little kid I’ve spend countless hours in front of my computer. But I wasn’t like all the other kids, oh no. When they were blowing each others brains out in Quake, I was madly in love with Visual Studio (the Enterprise version, of course) and Dreamweaver.

I wrote my first website when I was 12 years old, so what the hell am I doing working as an answering machine? I used to live for C++ and PHP. It was what I loved most and my biggest dream were to become just like Carmack and Thorvalds. Ah, those were some of the best days in my life, I tell you that.

What happened? I blame women. I blame sex. I blame the gym. Or maybe I should blame myself for losing focus of what I do best and love the most? Bah, it doesn’t matter anyway. What matters is that I’ve regained consciousness - I’m back.

Web developer is my title. It’s in my backbone. All I need now is a productive and friendly company to represent, my own desk to work at and a big-ass apartment to sleep in. Damn, this will be fun!

Amsterdam, here we come - me and my chocolate bunny girlfriend.

What Is Web 2.0?

There seems to be much confusion about what Web 2.0 is these days. A marketing word, a buzzword, sure - but what is it? Many people seem to think it’s a matter of design, layout and making a website dynamic. In some ways, I guess they’re right - but not really.

Design is not only how it looks. It’s also how it works. And dynamics is not just about how often a page needs to be refreshed. This is where people have gotten confused, as design is obviously seen as something purely graphical to most. Web 2.0 is not.

I would describe Web 2.0 as a concept by which you can design your site. It touches a range of different subjects. It defines the actual purpose of a web application and the way it works, in a much deeper sense than what you see on the screen and what you choose to click at. It’s not a recipe, it’s not a cookbook. It’s the whole concept of food, redefined.

Very abstract, indeed. But when you take a look at the big picture of how the internet looks today and where it’s heading, things get clearer. Let go of the past and the future will appear much less clouded, I guarantee you.

Look at Facebook and it’s increasing popularity (not to mention the $240 million or so investment from Microsoft.) Look at the way the blogging community looks and works. Look at Wikipedia and the subtle way it’s promoting collaboration on a massive and global scale. Flickr is changing the way people think and feel about photography. YouTube is doing the same thing with video.

The web is no longer about expressing so much as interacting. It’s about sharing information, communicating and collaborating, with people from all around the world, instantly. This is not something that can be fully summarized in an article - it is something that is occurring right now, constantly, all around us.

It’s a big yet subtle change in both the usability of web applications and the behavior if it’s users. Web 2.0 is another version of the concept of the internet. It isn’t just here, it’s all around us. Eric Schmidt is already talking about a Web 3.0, which is even more interesting.

If you want to read more about Web 2.0, I recommend this old but still accurate article by O’Reilly. And if you’re up to the challenge, I suggest you go define the next version. ;)