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:
- Download it from here (downloaded 10905 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














how use it ?
im just a beginner in this….but just wanna say i found this feed generator @ the web. Ist also easy to use and cool stuff if ure not into this. Just copy the rss link into the creator and choose your design…
nice, but 2 small things i noticed fast…
1. pls return your code as a string. so we can echo it ourselves or easily write it to a file. now everything is echoed by the functions
2. i think you meant to use the name of the function “generateFeed” not “genarateFeed” for the FeedWriter object
Where to get a PHP script which crawling all html pages on site and auto writing all new content into RSS?
Thanks.
Thanks, great rss feed generator
http://socialenginemechanic.com/groups/feed.php
Hi
Who of you are having problem with Non ASCII characters, can try this suggestion from ‘Nacho’.
See the line 289:
$nodeText .= (in_array($tagName, $this->CDATAEncoding))? $tagContent : htmlentities($tagContent);
Here the htmlentities function is being used with default options. You can see the php manual for list of charsets that are supported by htmlentities and can use your one.
how use it ?
Thanks for greate tools.
to use this class with none ascii characters change htmlentities to htmlspecialchars
@ Anis Ahmad: That would be line 298
. Thanks for pointing it out.
Just changing the:
htmlentities($tagContent);tohtmlentities($tagContent, ENT_QUOTES, 'UTF-8');will take care of all languages.Cheers,
m^e
hm,
validator offeres me an improvement to encrease compatibility in rss2
Missing atom:link with rel=”self”
how to achieve this?
Question here… I’m using your class in a project of mine and all’s working perfectly. Feeds are delivered from domain.com/feed/…
Now my question is, how do I detect (using PHP) which kind of feed is being requested – Atom, RSS or RSS2 ? As of now, I’m manually setting RSS2 while instantiating FeedWriter.
Googled for it but couldn’t come up with anything concrete.
Thanks,
m^e
Well, you could have a $_GET[] parameter and whichever the value is, generate a feed writer.
defeniately bookmarked
buraya birseyler karalamak gerekiyor ama tam olarak ne yazacagimi bilmiyorum.
What licence is use for this tool?
Thanks for this code. There’s a lot of typos in it:
$desciption for $description
genarate for generate
Univarsel for Universal
It makes the code hard to read and search.
Thanks for this useful piece of soft.
But please, insert the license in the code, since it’s required by the GPL to apply : http://www.gnu.org/licenses/gpl-howto.html
and it’s much easier to reuse your work.
Very informative content will subscribe to your RSS Feed.
[...] PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM) – Let’s explore the web te… (tags: rss feed generator php webdesign) [...]
Thanks for the valuable resource you have given, i have implementated to my portal. but still tuning it as per my need
thanks
Thanks for article. Keep up sharing.
Works like it says on the tin!
Perfect Thanks!
I’m not sure how other people were able to get this to work straight out of the box. The copy I downloaded from this site has a flaw that means none of the elements closed properly. To fix it change line 301 of FeedWriter.php to:
$nodeText .= (in_array($tagName, $this->CDATAEncoding))? “]]>” : “”;
The original code is missing the slashes:
$nodeText .= (in_array($tagName, $this->CDATAEncoding))? “]]>” : “”;
Hope that might save someone some time.
Sorry, that should read:
$nodeText .= (in_array($tagName, $this->CDATAEncoding))? “]]></$tagName>” : “</$tagName>”;
[...] PHP Universal Feed Generator [...]