nav-left cat-right
cat-right

PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)

It’s been a while since I’ve planned on developing a RSS writer that fulfills most my needs by supporting the various feed formats. Although the necessity was the prime force behind it, a discussion with Hasin and Emran has put the actual fire in. We were discussing about what can be added next in the Orchid – “PHP framework for the rest of us” and suddenly it hit me. At last, it’s finally complete and I’ve named it "PHP Universal Feed Generator", as it generates both ATOM and RSS feeds.

Supported versions:

  • RSS 1.0 (which officially obsoleted RSS 0.90)
  • RSS 2.0 (which officially obsoleted RSS 0.91, 0.92, 0.93 and 0.94)
  • ATOM 1.0

Download: 

Features:

  • Generates RSS 1.0, RSS 2.0 and ATOM 1.0 feeds
  • All feeds are are validated by feed validator.
  • Supports all possible feed elements.
  • Simple and easy to define channel and feed items
  • Implements appropriate namespaces for different versions.
  • Automatically converts date formats.
  • Generates UUID for ATOM feeds.
  • Enables usage of subtags and attributes. (example: image and encloser tags)
  • Completely Object oriented in PHP5 class structure.
  • Handles CDATA encoding for required tags.
  • Nearly same code for generating all kinds of feed

A minimum example

It’s a minimum example of using this class. I am generating a RSS 2.0 feed from retrieved data from a MySQL database. There are more examples in the download package for different versions.

<?php
  // This is a minimum example of using the Universal Feed Generator Class
  include("FeedWriter.php");
  
  //Creating an instance of FeedWriter class. 
  $TestFeed = new FeedWriter(RSS2);
  
  //Setting the channel elements
  //Use wrapper functions for common channel elements
  $TestFeed->setTitle('Testing & Checking the RSS writer class');
  $TestFeed->setLink('http://www.ajaxray.com/projects/rss');
  $TestFeed->setDescription('This is test of creating a RSS 2.0 feed Universal Feed Writer');
  
  //Image title and link must match with the 'title' and 'link' channel elements for valid RSS 2.0
  $TestFeed->setImage('Testing the RSS writer class','http://www.ajaxray.com/projects/rss','http://www.rightbrainsolution.com/images/logo.gif');
  
    //Retriving informations from database
    mysql_connect("server", "mysql_user", "mysql_password");
    mysql_select_db("my_database");

    $result = mysql_query("Your query here");

 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        //Create an empty FeedItem
        $newItem = $TestFeed->createNewItem();
        
        //Add elements to the feed item    
        $newItem->setTitle($row['title']);
        $newItem->setLink($row['link']);
        $newItem->setDate($row['create_date']);
        $newItem->setDescription($row['description']);
        
        //Now add the feed item
        $TestFeed->addItem($newItem);
    }
  
  //OK. Everything is done. Now genarate the feed.
  $TestFeed->genarateFeed();
?>

Shhhh….a universal feed reader is on the way ;)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Simpy
  • StumbleUpon
  • Technorati


130 Responses to “PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)”

  1. [...] به کمک این کلاس بسیار خوب php، که اسم بلند و برازنده ی PHP Universal Feed Generator رو داره، حالا [...]

  2. [...] PHP Universal Feed Generator es una clase escrita en PHP 5 que permite crear Feeds en formato RSS 2.0, RSS 1.0 y Atom 1.0 de forma sencilla y cumpliendo los estándar de los Feeds. [...]

  3. Rob says:

    What is the license? It’s kinda hard to evaluate if we should use this or not without knowing…

  4. kbraun GERMANY says:

    Like Kereste says (1. June) FeedWrite makes problems with some none-english utf-8 characters like ä etc.. this is because xml only knows 5 entities (&,”,’,). So I changed line 298 in the source code into

    $nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : str_replace ( array ( ‘&’, ‘”‘, “‘”, ” ), array ( ‘&’ , ‘"’, ‘'’ , ‘<’ , ‘>’ ), $tagContent);

    to avoiod converting charakters into wrong xml entities.

  5. Nacho says:

    Hello.
    I had a problem with feedwriter, normally only special char-languagues may suffer it but solved easily

    Line 289, function makeNode:
    $nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);

    Items titles doesnt have CDATA so it always pass the htmlentities function.
    Well, then the titles always shows the &code; char system, but in xml, specially in rss, i think that is better to use the numer char system, for example í for í, #243; for ó (there are some functions to made this in htmlentities php.net manual
    I think that the CDATA encodings must be always utf8 char type (with the encoding of document to utf8).
    For fix that, you must replace there the htmlentities of l 289 function by the one that you use for convert to xml char types.

    Sorry for my bad english. Bye.

  6. professional PAKISTAN says:

    Hello. I think you are eactly thinking like Sukrat. I really loved the post.

  7. [...] PHP Universal Feed Generator [...]

  8. hmongasia THAILAND says:

    Where I can see an example? Thanks

  9. [...] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) | Let’s explore the web tech… FeedWriter.phpの解説: [...]

  10. Thank You. Good work i wish

  11. Tugla TURKEY says:

    Info for the Thank You.

  12. Jojo GERMANY says:

    Lookin’ good, thanks a lot!

  13. Rammy UNITED STATES says:

    First off, LOVE THE SCRIPT. Nice simple and works great for most all applications I would need it for, so an big THANK YOU!

    I am having an issue though, I am trying to use this for an Itunes podcast. Here is the issue I am having. I tried just adding xmlns:itunes=”http://www.itunes.com/dtds/podcast-1.0.dtd” to the RSS2 feed and keep my type as RSS2. I also tried going through and creating a new ITUNES class copying the RSS2 class and adding xmlns:itunes=”http://www.itunes.com/dtds/podcast-1.0.dtd” but then removing it from the RSS2 class. When I do that (either option) it inserts the closing tags but without the backslash so technically they are all opening tags.

    Any suggestions?

  14. [...] PHP Universal Feed Generator es una librería para generación de feeds en varios formatos. [...]

  15. Nurul BANGLADESH says:

    Nice :) Thanks for sharing :)

  16. ali ISLAMIC REPUBLIC OF IRAN says:

    Hi,

    Thank you , i search and find your code is more useful than other codes for generate rss1,rss2,atom.

  17. [...] Feed Writer, Generador RSS en PHP 5 Universal Feed Writer es un excelente generador RSS desarrollado por Anis uddin Ahmad, en php 5 y con una estructura [...]

  18. Thanks a lot by this code… really I’ll use this on my site.

    Best Regards.

  19. [...] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)PHP Feed generator, 還沒用過, 不過看過範例, 覺得頗好用的, 先記下來。 話說我目前產生 RSS Feed 是用 template 的方式, 還滿方便、快速的。 [...]

  20. [...] bazý arkadaþlar oturmuþ rss 1.0 , rss 2.0 ve atom destekli bir feed generator (oluþturucu/üretici) hazýrlamýþlar. Kullanýmý da gayet basit.Zaten örnek olmasý açýsýndan küçük bir kod parçasý da vermiþler. [...]

  21. Thank you for developing feed generator! I used it to create a feed from items in a MySQL database and I am getting a couple of errors. 1) multiple items have the same value for link. 2) a problem with my date format. I see these when I run the feed (http://www.sph.umich.edu/scr/feedwriter/events_rss.php) through a validator (http://feedvalidator.org). Can you help?

  22. [...] 一些關於rss的東西 RSS 2.0 Specification PHP 產生 RSS/ATOM pubDate/created 所需格式 PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) [...]

  23. Thanks a bunch, I’ve just used this for creating a custom Atom feed!

  24. [...] searching for existing PHP scripts to accomplish this, and came across Anis uddin Ahmad’s Universal Feed Writer classes. They can be used to create RSS1, RSS2 and ATOM feeds. I extended his classes so that they [...]

  25. M. ITALY says:

    Hello,
    Great class! I am using it with SimplePie to merge some feeds. Is it possible to add more than one category to an item? Actually only one is added, even if I use a “foreach” loop…

    Thank you for the answer!

  26. Lucas Gonze UNITED STATES says:

    http://www.phpclasses.org is a bletcherous quasi-malware site. And your own link for it is 404. There must be a better way for you to host this.

  27. John Novak says:

    Are there any instructions on how to use this RSS Generator? Every time I try it I get a blank page. When I run the Feed Validator I get the following:
    Sorry
    This feed does not validate.

    line 1, column 0: XML parsing error: :1:0: no element found [help]

    In addition, interoperability with the widest range of feed readers could be improved by implementing the following recommendation.

    Feeds should not be served with the “text/html” media type [help]

    Source: http://beta.kavon.com/KAVONNewsCenter/RSS/FeedWriter/example_rss2.php

    Any help would be greatly appreciated.

    Thanks,
    John Novak

  28. John Novak says:

    One of the problems I got was the following:
    Use of undefined constant DATE_RSS – assumed ‘DATE_RSS’

    I am also getting 3 blank lines at the beginning of the xml code which is causing an error.

    Any ideas?

    Thanks,
    John Novak

  29. [...] ?????? RSS Feed ? PHP Class. ???? ? [...]

  30. [...] ?????? RSS Feed ? PHP Class. ???? ? [...]

  31. Kampanya says:

    Thank you…

  32. reklam says:

    Lookin’ good, thank you

  33. reklam says:

    Thank you very much good work for information

  34. [...] up with combineFeed.php (click for source)!  Its a pretty simple script that uses SimplePie and FeedWriter to pull in a bunch of RSS feeds, combine and sort them by date, then write them back out as a RSS [...]

  35. Peeprirwers says:

    Neat internet site. Will definitely visit soon.

  36. Guillaume says:

    Thanks !

    Very useful !

  37. dawid POLAND says:

    But polish chars doesn’t work…

    What i have to do if I want use polish chars ?

    I have this error:
    XML Parsing Error: junk after document element
    Location: http://www.example.com/feed/
    Line Number 3, Column 1:http://www.example.com/feed
    ^

  38. Nikola says:

    hi

    who is the best software for rss feeds creating ?

    thanks

    nikola

  39. tugla TURKEY says:

    Hi thanks…

  40. [...] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) – Let’s explore the web technolo… [...]

  41. Goran says:

    Great post. Thanks!

  42. Henry TAIWAN says:

    It seems not work for Chinese word.

Leave a Reply