I spent last weekend developing a helper PHP class to publish in a quick and
convenient way a RSS
2.0 feed. I developed it because I needed to publish RSS feeds in the new
cool web site I am developing (http://www.myvideodaily.com),
but I was not satisfied by other similar objects I found on the net. Since I
developed it in my spare time, I decided to publish it under the LGPL
At the moment it supports about 90% of the features of the RSS 2.0 standard, including feed image field. I will probably implement the missing features as soon as I will need them! ;-)
First of all download the class file RSS2Writer.class.php
and the sample file test.php
and store them in your web server.
Have a look at test.php source, it is very easy and you should not find difficulties in understanding how it works.
In any event, here follows a step by step tutorial.
First thing to do is to include class file source in your code, using include_once() function:
From now on you can use both the classes I created, which are RSS2Writer and RSS2Item.
You can create a new RSS feed creating a new RSS2Writer object, whose constructor has 3 parameters: the title of the feed, a HTTP Url to the web site and a description of the feed:
$rssw=new RSS2Writer("My Rss2 Feed","http://www.mauriziogiunti.it","This is a test feed");
Once you created the RSS feed object you can set optional properties using the set<name_of_the_property>() functions. At the moment I implemented the following functions:
function setLanguage($sLanguage) function setCopyright($sCopyright) function setManagingEditor($sEmail,$sName) function setWebMaster($sEmail,$sName) function setPubDate($sPubDate) function setLastBuildDate($sLastBuildDate) function setCategory($sCategory) function setGenerator($sGenerator) function setDocs($sDocs) function setTtl($sTtl) function setImage($url,$title,$link,$width,$height)
Please refer to RSS 2.0 official ducumentation for more info about each property meaning, but remember that all the input arguments are strings, except for the $width and $height arguments of the setImage() method which are integers.
Here follows an example of how to use set functions:
$rssw->setLanguage('en-us'); $rssw->setPubDate(date('Y-m-d H:i:s'));
Once you finished to set feed properties you can start adding real contents to it. You can add as many items you want to your feed using addItem() function. This function accept as argument an object of the RSS2Item class, so for each item you have to instantiate and populate one RSS2Item object as in the following example:
$item=new RSS2Item('This is a test item','http://www.mauriziogiunti.it/rss2writer.html','RSS2Writer PHP class'); $item->setPubDate("2009-07-24");
Once the item is ready you can add it to the feed:
As for the RSS2Writer class, RSS2Item object has some set functions:
function setAuthor($sEmail,$sName) function setCategory($sCategory) function setComments($sComments) function setGuid($sGuid) function setPubDate($sPubDate)
Once you added to the feed all the needed items you can get back from RSS2Writer object the XML formatted feed as string using its getAsString() method, but if you are going to push back the feed via http it's better, in terms of memory allocation, to use the doPrint() method, which simply prints the XML formatted file in the HTTP response stream. Usually you have to declare to the destination browser that you are not sending back an HTML document but an XML document, changing the HTTP header, so the final code would be:
header('Content-type: text/xml'); $rssw->doPrint();
Please note that in this case you should not print anything to the client before you set the HTTP header content type.
You can check the final effect of the sample
As I said there are some additional features of RRS 2.0 standard which were not yet implemented, properties like cloud or skipHours and skipDays, and I think I will implement them as soon as I will need them! But maybe some of you need them and can spend a bit of time to add those features to RSS2Writer. In this case please remember that this class is distributed under the LGPL license so you should share your addition and improvements to the code.
For any suggestions or question, please write to firstname.lastname@example.org.