Php RSS2Writer (v2.1) - Generate RSS 2.0 Feed - Usage Instructions & Download - - Web Development, Programming, SEO

Saturday, January 9, 2010

Php RSS2Writer (v2.1) - Generate RSS 2.0 Feed - Usage Instructions & Download

RSS2Writer v2.1 has been updated to v3.0 beta.  The new version can be downloaded at the Php FeedWriter website:

Php FeedWriter (v3.0 beta) now provides the ability to output to various feed formats.  Php FeedWriter can currently generate Atom 1.0, RSS 2.0, RSS 1.0, RSS 0.92 and RSS 0.91 compatible feeds

View more information about Php FeedWriter, including online documentation.  For information about the latest update and release, see the article Php FeedWriter 3.0 (beta) Released.

A tutorial demonstrating how to build a feed using Php FeedWriter is also available on this blog:
How to Generate Feed Using Php (Atom 1.0 and RSS 2.0 Compatible)

Generate RSS 2.0 Compatible feeds in Php, using the Free Php RSS2Writer Class.

The Php RSS2Writer class has been updated. Recently implemented v2.1 updates allow CDATA to be incorporated if required. Version 2.0 included improved usability when using the RSS2Writer class to generate an RSS Feed from website or database content, as well as additional functionality required to generate valid RSS 2.0 Feeds. Usage instructions for the class are below.

Download Php RSS2Writer - Version 2.1
The download page also provides some additional information about the class and sample controller.

Details of the Php RSS 2.0 Writer Package:

The package incudes the RSS2Writer class file that contains the functions and variables required to produce a valid RSS 2.0 compatible feed using Php.

The sample controller script provides examples for using the class and functions, including additional information about optional elements that can be added to a feed channel or individual item. Basic instructions are provided in the sample controller to help get started using the class.

Php RSS2Writer Usage
The following are instructions with examples for using the Php RSS2Writer class to generate RSS 2.0 compatible feeds using Php. The data added to the feed could be from a website, database, etc. that is accessible using a Php script.

Download the PhpRSS2Writer Package (v2.1) and copy the RSS2Writer.php file to your web server.

In the Php script that will serve the RSS Feed:

1. Include the RSS2Writer Class:



2. Initiate the an RSS2Writer Object:

When the RSS2Writer Object is constructed, the Title, Description and link for the Feed channel are provided as parameters when constructing the object.

$rss2_writer = new RSS2Writer(
'Feed Title', 
'Feed Description', 
'feed url', 
6, //indent
false //use CDATA

3. Add additional information to the Channel
(optional ) such as categories, copyright information, or the generator used to generate the RSS feed content.

//Add channel data to the feed
->addCategory("RSS Feed"); $rss2_writer->addCategory("Free Php Script");
$rss2_writer->addCategory("Php: Generate RSS 2.0");
//Optional Elements

->addElement('copyright', '(c) Daniel Soutter 2010');
->addElement('generator', 'Php RSS2Writer by Daniel Soutter');

4. Add Items to the Feed

Add items to the RSS feed using the addItem function, which takes the title, description and link for each item. Once the item has been added, the addCategory, or addElement functions can be called to add categories, or other optional elements to the feed item that sore information such as the name and email address of the author, and the date that the item was published.

//Example Item
$rss2_writer->addItem('item title', 'item content/description', 'item url');

//Add categories to the item

$rss2_writer->addCategory("Free Php Script");
$rss2_writer->addCategory("Php: Generate RSS 2.0");
$rss2_writer->addCategory("Php RSS2Writer Usage Instructions");
//Optional Elements
$rss2_writer->addElement('author', ' (Daniel Soutter)');

5. Output the RSS Feed XML

Output the RSS feed XML for use in a web browser or application that syndicates RSS feeds.

echo $rss2_writer->getXML();

The addCategory and addElement functions can be used to add categories or additional elements to both the feed channel and individual feed items. To add additional information to the feed channel, call the functions before adding items to the feed. Once items have been added to the feed, calling the addCategory or addElement functions will add the specified information to the feed item most recently added to the feed.

There are a few functions which can be called at any time to associate an image to the RSS Feed Channel, or include channel Cloud data, for use by RSS Feed readers and other applications when retrieving and presenting data from your feed.

The channelImage() function associates an image with the RSS Feed Channel, passing a title, link, url, and the dimensions of the image (width, height).
$rss2_writer->channelImage($title, $link, $url, $width, $height);

The channelCloud() function adds cloud information to the Feed Channel. The domain, port, path, registerProcedure and protocol are passed to the function. The Port, registerProcedure and protocol paramaters use '80', 'pingMe' and 'soap' as defaults.

$rss2_writer->channelCloud($domain, $port, $path, $registerProcedure, $protocol);

If you have any queries about, or issues with using the Php RSS2Writer class, please leave a comment on this post. Suggestions for improvwement or additional functionality are welcome as well, and will be incorporated into new versions of the class if appropriate.


  1. Hi

    My name is Abdul Basit

    I have used this code (RSS2Writer Class).

    I have a problem in 1 place i.e.

    How can i put tags of CDATA.?

    I have to show description and it contains characters.

    Plz tell me how to do that.


  2. Hi abasit83,

    When creating a new RSS2Writer object from the class, or are adding an item to the feed, you should be able to do this by wrapping the data inside CDATA tags.

    For example, when calling the addItem function, you would include the CDATA tags in the description (2nd) paramater:

    addItem($title, '<![CDATA[' . $description . ']]>', $link);

    Let me know if you have any issues, or if the class file requires any modification to allow CDATA tags to be included.


  3. HI

    I have tried as you said but when i put the code

    it only gives me "Title" with link and "Publication Date". Means "Description" hides.

    So what to do?

    Abdul Basit

  4. The above discussion between myself and abasit83 was taken offline for resolution. Abasit83 was able to successfully incorporate CDATA content in the feed using the RSS 2 Writer class, and also provided the changes made to the class.

    I am now updating the class files to include the CDATA mods written by abasit83 as an option when constructing the RSS 2 Writer object, and will update this post and download file shortly to include the changes.

    Thanks abasit83.

  5. Troubleshooting notes for the CDATA issue have been added in this post: RSS Feed CDATA Troubleshooting

  6. Hi Daniel. I use CMS Made Simple and Coppermine Photo Gallery. Shall I put the code from sampleController.php into for example my templates and call RSS2Writer.php from there?

  7. Hi

    My name is Boris, I want to use this RSS2Writter class, but I have to use values from a database, so Can u help me how to do that?
    Thanks again


  8. Thanx Daniel for the positive comments..


  9. Hi Asce,

    I'm not familiar myself with CMS Made Simple or Coppermine Photo Gallery, but it sounds like the templates would be for the layout of the interface only.

    You would probably need to include create a new page that serves RSS Feed data that uses the methods/functions of the application you are using to obtain the data to be added to the feed. You may be able to incorporate into an existing page, but you would need to allow the page to render only the RSS XML containing the content instead of the html layout. This would probably be done using query string parameters, but creating a new script/page for the RSS feed would take a similar amount of time and effort and has less risk of impacting on the working application.

    If you examine the scripts called by the application to display content, you should be able to render the same content as an RSS feed using the PhpRSS2Writer class.

    Please let me know if you need further explanation.

  10. How to add html element (as b,i,img,etc.) to the item?

  11. Hi Ladislav, you can include html formatting using html encoded characters.

    For example:
    <img src="http://image_url/">

    would need to be be included as:
    &lt;img src="http://image_url/"&gt;

    When viewing the feed, the html will be rendered as a web page would. To include html markup as text in the feed item, CDATA should be enabled as well as encoding special characters.


  12. Hello,

    I am attempting to use the enclosure element to add an image to my feed. This does not seem to be working or have any real documentation. Has this been implemented?

    Thank you.

  13. Hey DanielS,

    I am new to RSS. I am creating rss for books. I simply copied both your files and put it in my development server. Code works fine for Firefox. But it doesn't work for IE and chrome. (IE simply doesnt show up anything and chrome shows only text data not formatted.)

    I tried to put header("Content-type: text/xml"); but it still doesn't work on either one of the browser. Am i doing something wrong?


  14. Hi Ketav,

    I haven't tested the solution using Chrome, but haven't had the issues you describe with IE(7).

    Try enabling CDATA (final parameter when instantiating the RSS2Writer object), and if you are adding your own content to the feed ensure that it is encoded properly.

    Please let me know how you go, and if it doesn't help, I will try to replicate the issue myself and resolve in the next update. The class was written a while ago now, and needs a bit of love.

  15. @drewread, associating an image to the feed was intended to be incorporated using the channelImage() function. Although included, it was wasn't a core feature of the original implementation, and has no online documentation as a result.

    I will do some tests around image association myself and include any changes in the next version. I will also include some documentation around the function once tested and implemented properly.

    Have you had any success since posting your comment?


  16. @Boris,

    Once you have the feed data from the database, you loop through the result set and call the addItem() function to add a feed item for each row in the database query result. Use the data from the current row (loop iteration) in the result set to populate the feed item.

  17. hello thanks for the script i want ask it's possible add also an image in Item instead of channel

  18. Hi i have found that is possible add an image inserting in description item
    However i wait your answer
    Also i want ask you if is possible insert following schemas in items:

    image title

    with image inserted at left of title

  19. Hey DanielS,

    I got the code working. Actually there was no issue with code. Every browser requires Rss reader program. I installed it in chrome and then script was working fine. About IE8 i don't remember exactly but i changed some settings in IE8 and script is is showing correct output.

    Thanks very much for the script.

  20. @Jack, I will have a look at the class in the next week or so. I have been struggling to find time lately due to my current workload.

    Just to clarify, you would like to have the ability to associate an image with an item in the feed instead of the channel, and with the three optional elements for the url, title and description.

    I had a quick look at the RSS 2.0 Specification on W3C, and also a thread here, which talks about including images in feed items.

    The image element is a sub-element of the channel element only. It looks like an image needs to be included using the <enclosure> element. The feed viewers that I use all list attached media at the bottom of each item, which are included using the enclosure element.

    Alternatively, the image can be included in the description/body of the feed item as html. Using this approach, functionality could be added to the class to insert image html into the feed item description, which can displayed in the top left, but it would be under the title.

    Would you find this useful as an alternative to the enclosure, or would you prefer to use the enclosure method (which should be incorporated into the class regardless)?

    Are you aware of other methods of associating an image with a feed item?


  21. ok I have resolved for the moment inserting image in the description/body of the feed item as html.
    However when you have time for complete your feed generator can you build a function to insert css:
    and i don't know also if is possible add also javascript code in xml feed?
    thanks bye

  22. In order to get channelImage() to work, I had to change $rss2_writer, in the example to $rss. It doesn't seem like the width and height work though.

    Now I'm trying to figure out how to associate a image with each item. It sounds like it requires the enclosure tag.

    Also, feed validator keeps telling me "Feeds should not be served with the "text/html" media type".

  23. Hi Daniel,
    First of all congrats on writing a good class of RSS.

    I would like to suggest an addition to the class, please add it in your future versions if helpful.

    ok, recently i was developing a website for my client and i used your class for generating dynamic RSS feeds. The feed data is based on recent posts by users, so their is less control over the feed data. i.e.

    ISO 8859-1 Symbols, ISO 8859-1 Characters are directly pasted by users and not encoded. characters like copyright or registered trademark symbols etc

    the getXML function generates the feed but it ends up in error as feed readers generate a parse error.

    i have updated 2 lines in get XML, previously updated by abasit83.

    line 310: $writer->writeCData($item['title']);
    to $writer->writeCData(htmlentities($item['title']));

    line 349" $writer->writeCData($item['description']);
    to $writer->writeCData(htmlentities($item['description']));

    This will encode the characters to entity name.

    Muhammad Khalid.
    PHP Software Engineer.

  24. Due to the recent increase in use of the RSS2Writer class, I am getting the next major update underway. I propose that the solution and supporting documentation also be moved to a new dedicated site, which will provide areas for the development community to discuss aspects of the class, suggest ideas for future development and any usage issues. The class will also remain available from this blog.

    I intend for the class (including future versions) to remain a free resource for Php web developers, as long as I continue to receive support from the community. A reasonable amount of my time has already been spent developing and supporting the class, which I hope to continue as a community effort. The time I have spent on this class (and my blogs) is my own time (out of work hours). Any help from others to speed up the process of improving the class would be greatly appreciated. Thank you for those who have contributed so far.

    I will consolidate the suggestions for improvement made on this post for inclusion in the update, and will attempt to include suggestions posted in the near future as well. I will include comments in the code with the name of someone who has provided input where possible, unless specifically asked not to be included.

    One of the enhancements that I personally intend to include is the ability to output the feed data in more than just an RSS 2.0 compatible format. This will also result in the newer version of the class having a more general name such as FeedWriter, or similar. Previous versions of the class will remain available.

    In the new version the getXML() function will probably remain, but it’s functionality will be moved to 3 additional functions such as getRSS1(), getRSS2() and getAtom(). Each method will output the feed data in the corresponding format. I will attempt to make functionality in the new version backwards compatible (same function names and core functionality), apart from the new class name.

    Please let me know (soon) if you have any suggestions or concerns.

  25. I've noticed several of my descriptions, that are being pulled from a database, include relative href and src links. If possible, it would be pretty cool if the class could check for those links and change them to absolute.

    Also, to expand on what Muhammad mentioned. It would be nice to not only check for trademarks, copyrights and registrations, but also smart quotes, elipsis, em and en dashes, one quarter, one half, three quarters and ampersands.

  26. @jjames, do you mean absolute by including the domain name before urls without it, or to convert relative paths (eg. "../page1.php", "page2.php", ../../somefolder/page3.php ) to absolute.

    The issue that I see is that it would be difficult to programmatically determine the path back to the root of the site, which would be required in order to build the absolute path. Additional contextual information would need to be provided at some point when populating the feed to allow the class to update paths to absolute.

    On the other hand, changing only absolute paths that are missing the domain component to include the domain would be easier to implement, and should have more consistent results.

    I would probably implement this by finding links in the feed description with a href attribute that begins with "/" and including the domain and protocol before the existing address.

    What do you think?

  27. Ok, the class update is coming along now...

    So far, I have incorporated the following into the class:

    - New class name
    - Add functionality to include media files in an enclosure element.
    - Fixed content type issues (@jjames)
    - Added option stylesheet integration (@Jack)
    - Included character encoding changes (@Muhammad)

    I am part way through RSS 1.0, and Atom 1.0 compatible XML output formats, which I will completed once all other updates have been incorporated, to allow code re-use.

    I hope to publish a beta version soon for others to try.

    @Jack, I can't see an example or documentation anywhere for an RSS feed with JavaScript integrated. There are plenty of resources for rending RSS feeds using JavaScript, but not to include JS in a feed.

    Do you (or anyone else), know if this is included in the RSS 2.0, or Atom schemas?



  28. I was just thinking about searching for url's that were missing the domain name. E.g. images/mypicture.jpg. But, you're right, it could get difficult, and I'm not sure if it's really needed in most cases.

  29. Just a quick update..

    The v3 update is close to completion, which has involved almost a complete work-over. A beta release will be first made available for others to try, which is likely to contain glitches. I am also working on documentation that will accompany the new version.

    It currently outputs RSS 0.91, RSS 0.92, RSS 2.0 and a not quite valid Atom 1.0 format (currently working on). RSS 1.0 (RDF) output is also supported, but not yet functioning properly.

    The construct of each feed format is configured in a new class, which allows each element, including validation constraints and attributes to be added. Functions provided by the class allow each feed construct to be traversed in a tree/hierarchical manner. This is used to both validate and output a feed in a particular format using generic code.

    Looking forward to it's completion..

  30. Hi,

    Version 3.0 (beta) has been released. This now includes the ability to output Atom 1.0, RSS 2.0, RSS 1.0 (RDF), RSS 0.92 and RSS 0.91.

    The main class file has also been renamed to FeedWriter as the solution is no longer limited to only RSS 2.0. The solution as a whole is now Php FeedWriter, which starts at version 3.0 (beta) and is the next major update to the RSS2Writer class.

    For more details, please see my post: Php FeedWriter 3.0 Beta Release, or the Php FeedWriter Website. Version 3.0 (beta) is available from the website.

    The website also includes documentation for each component of the FeedWriter and new FeedConstruct class.


This website is using cookies. More details