Saturday 14 Jun 2008

Switch from RSS to Atom feeds

atom-enabledWhat we generally call RSS feeds can be in fact feeds from different formats: RSS 1 (or 0.9), RSS 2 or Atom.

The last one has an advantage (very important for a real geek) on the RSS format: it's not proprietary and supported by the W3C. ;-) Moreover it brings additional elements for more content.

That's why I decide to put the link on the Atom feed on my blog. Here is how to do:

With WordPress the 3 versions are generated.

  1. azure-dev.kizone.net/feed gives RSS 2 feed,
  2. azure-dev.kizone.net/feed/rss gives RSS 0.92 feed,
  3. azure-dev.kizone.net/feed/atom gives Atom feed.

If there are links to the RSS 2 feed in your theme you can easily replace them by /feed/atom. And for the main feed in the browser address bar, it's in the header (generally the header.php file in the theme).

You must write in first, or suppress the 2 others, the link to Atom:
<link rel="alternate" type="application/atom+xml" title="Atom feed" href="http://azur-dev.kizone.net/feed/atom" />


PHP

If you build yourself your own RSS feed from your site, the best is to manualy write the code. I tried a existing class to generate feeds, but it's more complicated than generate it myself! :-p

~

Atom feed example for you to create it with PHP or other language

This is the file header with your identity:
<?xml version="1.0" encoding="utf-8"?>
(<?xml-stylesheet href="rss.css" type="text/css"?> to add a CSS style)
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Atom feed of the site URL.com</title>
<link rel="alternate" type="text/html" hreflang="fr" href="http://URL.com/" />
<link rel="self" type="application/atom+xml" href="http://URL.com/atom" />
<updated>2008-06-14T22:48:23+02:00</updated>
<author><name>URL.com</name></author>
<id>http://URL.com/</id>
<subtitle>The last articles of URL.com</subtitle>
<icon>http://URL.com/icone.png</icon>
<logo>http://URL.com/logo.png</logo>

~

Then each article must appear on that model. If the articles are in a database, a request and a loop, and it's done. :-)
<entry>
<title>Title of my article</title>
<link href="http://URL.com/article-link" type="text/html" />
<id>http://URL.com/article-link</id>
<summary type="html"><![CDATA[Excerpt of the article...]]></summary>
<updated>2008-06-14T20:08:08+02:00</updated>
</entry>

To "summary" we can add a "content" which will be more complete, even the entire article if you're not affraid the readers don't be forced to come to your site to read it entirely (i.e. if you don't have ads on your site!). :-)

The link is written twice because "id" can be a diffrent identifier, but the simpliest is to put the link. It allows RSS readers to easily identity the readen posts.

More options are of course available, see all Atom specifications.

You must only pay attention to the dates format, a bit strange (normalized by the W3C). The rest is very close to RSS 2.0.

~

Be careful, you must send it with the good MIME type (subject of a previous article on the MIME type for CSS and Javascript):

header('Content-Type: application/atom+xml; charset=utf-8');


You can finaly test your feed on Feedvalidator or the W3C validator (they are clones, the result will be inevitably the same on both).


cafĂ© Did this article help you? 
Buy me a coffee!

6 answers at “Switch from RSS to Atom feeds”

  1. 1
    guruz (blog.guruz.de) said:

    OK, but are there any REAL advantages except w3c-compliancy? Anything that is visible to an end user?

  2. 2
    David (azure-dev.kizone.net) said:

    Honestly for all courant use, I must say no. Atom is very close to RSS 2.0.

    You will just be proud to participate to the effort of standardization of the web. ;-)

  3. 3
    guruz (blog.guruz.de) said:

    which of course is a good thing too =)

  4. Backlink from Images in PHP with GD2 - Azure Dev
  5. 4
    required said:

    benefit - image enclosures

  6. 5
    David (azure-dev.kizone.net) said:

    Both RSS and Atom can have images.

Leave a comment (all comments are moderated, don't waste time with spam)

Azure Dev