PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)
ATOM, writer, rss, feed, tools, php March 8th, 2008It’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:
- Download it from here (downloaded 1444 times).
- Download from phpclasses.org.
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











March 8th, 2008 at 2:00 pm
[…] : The PHP Universal Feed Generator class, for which I wrote this function is released. Share and Enjoy: These icons link to social […]
March 9th, 2008 at 8:21 am
Hello there.
Did a quick look and I have a few comments:
- It should be “generateFeed” and not “genarateFeed”
- The constants would be better as class constants imho
- The __autoload has nothing to do there and will be annoying for people using your code (they’ll have to remove it)
- (Almost) everything private should be protected instead to allow for inheritance
- What is the license? It’s not written anywhere
- I’m not very fan of all these concatenation, 3 template files (1 for each type of feed) could be clearer and less bug-prone
Good work.
March 10th, 2008 at 7:06 am
Hey there,
Just wanted to let you know that you should test your site in Safari, because the whole main body container is flipping to the left out of alignment with the header container. Its also noticeable on Firefox, although its not nearly as bad as Safari.
Great post by the way.
March 10th, 2008 at 7:32 am
I do not find if this is not the best code, the main thing is that it works! Thank you very much Loic when you do some developments let us know to check if you create code as you criticise
March 10th, 2008 at 7:42 am
[…] estos casos en Ajax Ray crearon un código php que utilizando simplemente las clases correctas podemos generar un feed valido prácticamente desde […]
March 10th, 2008 at 8:03 am
@Loïc Hoguin:
Thanks a lot for your suggestion. I’ll modify it soon as your comment.
When the createNewItem() function is called, the __autoload() function loads the FeedItem class from FeedItem.php file.
Thanks you again.
March 10th, 2008 at 8:05 am
@Jake Rutter:
Thanx for you comment.
I’ve tested the site in safari 3.0.4 and no problem found there. Can you plz tell me about your Safari version?
March 10th, 2008 at 8:44 am
great post. i guess this will solve most people’s need of a good rss writer class. thanks for mentioning me
March 10th, 2008 at 9:10 am
Specially happy to make it part of Orchid
March 10th, 2008 at 9:15 am
Yes, the __autoload is there for loading the item class, but what I was trying to point out was that if I include your feed generator while already having an __autoload in my project, it’ll die because the __autoload function already exist. So I would have to remove that part.
But that’s just a small annoyance, don’t worry about it too much.
Have fun polishing it!
March 10th, 2008 at 9:34 am
To do an autoloading behavior that plays well with others, you should use spl_autoload_register() (http://www.php.net/spl-autoload-register). With that, you can register any valid PHP callback you wish to serve as your autoloader.
March 10th, 2008 at 10:18 am
[…] [view original post] [source: Delicious] Previously - WordPress Upgrade Preparation Checklist : The Blog Herald Next - […]
March 10th, 2008 at 10:59 am
simplepie.org is a great universal feed reader, btw
March 10th, 2008 at 11:17 pm
[…] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) (tags: php) […]
March 10th, 2008 at 11:43 pm
This worked fine for me. Cheers mate.
March 11th, 2008 at 2:10 am
[…] Fonte: http://www.ajaxray.com/blog/2008/03/08/php-universal-feed-generator-supports-rss-10-rss-20-and-atom/ […]
March 11th, 2008 at 2:10 am
[…] Fonte: http://www.ajaxray.com/blog/2008/03/08/php-universal-feed-generator-supports-rss-10-rss-20-and-atom/ […]
March 11th, 2008 at 9:49 am
[…] PHP Universal Feed Generator […]
March 11th, 2008 at 1:28 pm
[…] PHP feed generator. Librería en PHP para dotar de feeds RSS o ATOM a tus proyectos web. Visto en Sentido Web. […]
March 12th, 2008 at 11:58 am
[…] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) | ajaXray - Haven't looked to see if this is any good yet. Having something like this is good from PHP though. Tags: atom feed php rss […]
March 12th, 2008 at 4:21 pm
[…] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) | ajaXray (tags: php generator feed opensource programming) […]
March 14th, 2008 at 3:38 am
[…] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) PHP Feed generator, 還沒用過, 不過看過範例, 覺得頗好用的, 先記下來。 話說我目前產生 RSS Feed 是用 template 的方式, 還滿方便、快速的。 […]
March 16th, 2008 at 7:23 pm
Thanks a lot for your code !
But where is the .xml generated ? (I’m beginning …)
Thank you !
March 16th, 2008 at 11:15 pm
@Romain:
Hi friend,
Thanks for your comment.
There is no need to have a separate file with .xml extension. When you call the generateFeed() function, it sets the header Content-type as “text/xml”. So, the browser handle the output as an XML file.
March 17th, 2008 at 7:35 pm
[…] php universal feed generator […]
March 18th, 2008 at 6:14 am
[…] PHP Universal Feed Generator es una librería para generación de feeds en varios formatos. […]
March 19th, 2008 at 9:42 pm
Anis vai,
I’ve tested the examples and found the following error
http://localhost/feedwriter/example_minimum.php
Fatal error: Call to a member function query() on a non-object in C:\apachefriends\xampp\htdocs\feedwriter\example_minimum.php on line 18
Would you please check out this.
By the way, all the things are appreciatable. But I’ve two suggestions for you.
1) FeedWriter.php: In function startItem (line 398) you’ve used die. But anyone may want to handle exception according to his/her necessities. So, throw new Exception() should be used here.
2) FeedWriter.php: You’ve used echo in several functions to generate Feed. But I think it would be better if you assign all the echos in a string and return the string to the caller. As for example, anyone can write echo $TestFeed->genarateFeed(); (See example_rss1.php line 44).
Thanks.
March 20th, 2008 at 12:30 am
@Mohammad Jahedur Rahman :
Jahid vai,
Thanks for your comment.
Here the “$db->query()” is just an example of using.
There is no database connection and no $db class in this script.
You have to re-write this data collecting portion yourself as your database.
I’ll keep in mind your suggestions and try to implement in next version.
March 20th, 2008 at 8:16 am
[…] PHP Universal Feed Generator Librería que nos permite añadir feeds en varios formatos en nuestras aplicaciones. […]
March 26th, 2008 at 7:42 am
it gots some problems with chinese character.
SO I modify some source code.
In Line 298
htmlentities($tagContent);
switch
htmlentities($tagContent, ENT_COMPAT, ‘utf-8′);
In Line 82
header(”Content-type: text/xml”);
switch to
header(”Content-type: text/xml; charset=UTF-8″);
March 26th, 2008 at 7:56 am
It got some problems with chinese character.
so I modify some source code.
at Line 298
change htmlentities($tagContent);
to
htmlentities($tagContent, ENT_COMPAT, ‘utf-8′);
at Line 82
change
header(”Content-type: text/xml”);
to
header(”Content-type: text/xml; charset=UTF-8″);
that’s it
March 27th, 2008 at 8:02 am
how do I link it to the mysql DB? I cannot make sense about that? How do I pass the user/pass and select with dB/tables to query? Could you please explain that a lil bit more?
Thanks! great script!
April 2nd, 2008 at 1:52 pm
@sergio:
Hi friend,
U and some of others were confused at this portion.
So, I’ve changed it.
Hope it will be easier now to understand how to collect informations from db and use with this class.
Thanks a lot.
April 15th, 2008 at 8:39 am
[…] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) | ajaXray […]
April 22nd, 2008 at 3:08 am
it can run in php4 ??!!
because run in php5 is ok
but php5 have an error
”
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /www/history/FeedWriter/FeedWriter.php on line 20
“
May 2nd, 2008 at 9:49 am
[…] the PHP Universal Feed Generator, I’ve written the PHP Universal Feed Parser for Orchid Framework. It’s a RSS and […]
May 13th, 2008 at 2:07 pm
[…] Feedwriter / PHP […]