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/Clone/Fork it from here.
- 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://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://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 😉
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.
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.
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
@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.
@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?
great post. i guess this will solve most people’s need of a good rss writer class. thanks for mentioning me 🙂
Specially happy to make it part of Orchid
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!
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.
simplepie.org is a great universal feed reader, btw
This worked fine for me. Cheers mate.
Thanks a lot for your code !
But where is the .xml generated ? (I’m beginning …)
Thank you !
@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.
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:apachefriendsxampphtdocsfeedwriterexample_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.
@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.
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”);
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
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!
@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.
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
“
For knowledge thanking I do
Great idea! I got here after this class was listed on a post at PHP::Impact. I look forward to working with this class in the future.
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
it gots some problems with chinese character.
SO I modify some source code.
In Line 298
Thanks for your comment.
Here the “$db->query()†is just an example of using
What is the license? It’s kinda hard to evaluate if we should use this or not without knowing…
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.
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.
Hello. I think you are eactly thinking like Sukrat. I really loved the post.
Where I can see an example? Thanks
RSS 2.0 feed with PHP
http://usedauto.com.ua/
Thanks
Thank You. Good work i wish
Info for the Thank You.
Lookin’ good, thanks a lot!
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?
Thanks
Nice 🙂 Thanks for sharing 🙂
Hi,
Thank you , i search and find your code is more useful than other codes for generate rss1,rss2,atom.
Thanks a lot by this code… really I’ll use this on my site.
Best Regards.
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?
Thanks a bunch, I’ve just used this for creating a custom Atom feed!
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!
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.
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
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
Thank you…
Lookin’ good, thank you
Thank you very much good work for information
Neat internet site. Will definitely visit soon.
Thanks !
Very useful !
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
^
hi
who is the best software for rss feeds creating ?
thanks
nikola
Hi thanks…
Great post. Thanks!
very nice great post
It seems not work for Chinese word.
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.
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>”;
Your posts always show me that you really have some indepth knowledge about this. Quite a valuable read i must say
thnaks for sharing
How do I add an image inside the description tag?
This is a very beautiful and useful program. This code can easily prepare the RSS link on my pages.
It worked as intended. Took less than 10 minutes of implementantion to get it working, retrieving info from my db and writing the feeds.
Thanks a bunch!
If anyone is still looking to add the feed validator’s atom:link suggestion: change the genarateFeed function in FeedWriter.php as follows:
/**
* Genarate the actual RSS/ATOM file
*
* @access public
* @return void
*/
public function generateFeed()
{
header(“Content-type: text/xml” . ‘; charset=utf-8’);
$this->printHead();
$this->printChannels();
echo ‘channels[‘link’] . ‘” rel=”self” type=”application/rss+xml” />’;
$this->printItems();
$this->printTale();
}
I also corrected the function name type and added charset=”utf8″. This is just a quick fix if you only generate RRS2 feeds. Some refactoring is in order for a real fix of course.
Hi, I’m using the RSS 2.0 and it seems tobe working alright, but for some reason it doesnt appear right on IE 7. IE just displays XML rather than in HTML format. Has any of you came across this problem too?
???? ?????? ???? ???? ?? ????????!
This is a very beautiful and useful program. This code can easily prepare the RSS link on my pages.
Thanks for sharing this, saved me lots of time.
Thanks for the script… now just need to get it work on my site… there is no readme file with short description?
RSS Feeds are really very helpful and you could get site and news updates from it.,`;
Creating your own RSS feed is a pain in the butt, I am glad others make scripts to complete this task.
1.
You use htmlentities() to encode text that goes into non CDATA fields; this produces illegal (undeclared) XML entities.
You should only encode ‘ ” &
2.
You declare encoding=”UTF-8″ in the XML header, but this is entirely platfor dependant. You should add utf8_encode() to be sure that the encoding corresponds to the content, else accented characters will produce errors.
…and if you could correct all the spelling mistakes… 😉
But this is still a nice library – thanks!
Missing atom:link with rel=”self”
how to achieve this?
I tried for over an hour but couldn’t get this script to work. Bugs in FeedWriter.php and elsewhere. Looks great but doesn’t seem to work 🙁
Is this script out of date?
HI, great post.
but how can I use ISO-8859-1 ?
It’s a great solution to get RSS feed implemented in your web site
Hi I’m new to this kind of thing so please bear with me. How do I incorporate this into my site? Do I just follow the way you encoded the information in the examples? Please help.
excellent !!!!
Hi, Thanks for such a wonderful script. Can you tell me how it works?
RSS feeds are really great because you are always updated with the latest news or blog posts.*-*
thanks for this script, had a little problem when trying to use it for the first time, but got around it
I use atom feed but i have problem with the Greek characters, i m trying to load it from my database and it give me back the letters as Special characters(like: &beta) on “View page Source” but that happen only to , on the is everything ok. I have tried to chance the live 298 with this code: htmlentities($tagContent, ENT_QUOTES, ‘UTF-8’); and i don’t know why but somehow it strip the slashes from the tags. Anybody know how can i fix that?
Thanks in advance.
How can add this feed creator in my website
Will this class also take care of cleaning the data. Like if you have for instance data that holds html or even worse xml in it will it make sure it won’t break the rss feed produced?
RSS feeds are really great if you want to stay updated ;*,
Tested the script and it works wonderfull
I use you class to make my rss.
When I tried to validate with feedvalidator says:
“item should contain a guid element”
How I can solve this issue?
Another question:
I’m using utf-8 at my server and my language is spanish. When I put any character with accent the feed don’t work.
Have you any solution?
Thanks in advance.
Using this on Mythtv and if the any of the titles has & characters in it the resulting page will be completely blank.
Thanks for the script. I am really impressed and have been looking for something like this!
i use RSS Feeds to syndicate my blog on other blogs*`-
Outstanding! Quick setup and simple to use. Just the way it should be.
RSS feeds are necessary for transmitting your blog updates to your readers or followers:~*
Thanks a lot Sir for this excellent script that will save me time 😉
I use this script on all my sites – Outstanding!
Hi There,
Thank you for the class, works really great (after a couple of minutes of tweaking and setting up)…
Just one thing I would like to confirm. Your closing tags for the items, none of the close with or etc… Is this suppose to be like this? I was unable to get the feed to work until I modified the FeedWriter.php file on line 304 : $nodeText .= (in_array($tagName, $this->CDATAEncoding))? “]]>” : “”;
It works perfectly now.
Ok, your blog strips out code so my previous comment will make absolutely no sense at all. Basically would like to know why your closing tags on line 304 of the FeedWriter class do not start with a backslast ? I only got it to work after changing your class 🙁 But thanks again for an excellent class!
Really like this, saved me at least three days of work! 🙂 Thanks m8!
Simple to integrate to my current PHP code and works very well!
thanks for the work on the libary. may i note that it’s “generate” not “genarate”?
i think that RSS FEEDS should also be included on the list of the best inventions because it makes life easier for bloggers like us ,””
Recently I did a search on the issue and found a great number of persons will agree with your blog!
Great!
I have appreciated your current article keep up the great work. This particular subject matter provides always fascinated myself.
for displaying non-english characters in item title, don’t forget to change line 42 to:
$this->CDATAEncoding = array(‘title’, ‘description’, ‘content:encoded’, ‘summary’);
How do I change the link address so that it has the format ‘article.php?ID=xx’ where xx is the NewsID from database table?
Which file do I edit?
Your a lifesaver!
This will make it easier for me to update my feeds 🙂
Combining your script with Feedburner updates my Facebook & Twitter profile on the fly.
Outstanding information! I’m glad to know there still are some real blogs these days which are truely worth reading.
-.’ I am very thankful to this topic because it really gives great information *”.
Thank you so much for making this AND documenting it well.
I used it for a unique situation where I had to pull XML from weather.gov into a PHP XML parser, create an RSS, and post it to an .aspx website.
Howdy blogger, thank you for providing this article.. I found it first-class.
Awesome Script…
Thanks for the blog strips.there is something very unique and enjoyable about.
Thank ! Great change and I am glad that you posted a workaround for us using Mantis partially inside a frame . However having configurations options for this would a be a real highlight.
Looks good; thanks for sharing it Have you thought about publishing your code on github (or someplace similar)? It is quite nice for collaboration, and allows you to easily evaluate and merge suggested fixes / improvements.
Thanks buddy, you saved me three hours and now I have a decent generator rather than whatever basic thing I’d have come up with 😉
And what about sitemap ? Does it support to create sitemaps ? 🙂 If yes, would be excellent.. Thanks anyways
It is looking like excellent. Thanks.
Thanks so much for this. I need a rss feed for my website badly.
@Brian: Just found out this code is already on github:
https://github.com/ajaxray/FeedWriter
(I’ve forked and changed the link — ajaxray)
Sorry the download link is down. Added github link.
Phpclasses seems to have updated and working structure of files.
the RSS generated from zip downloaded from phpclasses shows error on validation from w3c
“”””This XML file does not appear to have any style information associated with it. The document tree is shown below.””””