Jul 142006
Feedcreator – make your newsfeeds the easy way!
This website has been providing news feeds since at least 2001. I was recently asked to provide an ATOM or RSS2.0 compliant news feed. The format has not been updated since the initial implementation. Rather than code all this myself, I wanted to concentrate on getting the data, and letting someone else code the right format. It makes no sense to roll-your-own when you can let someone else do it for nothing. I went searching for PHP based news generation classes. I found many news aggregators (packages that pull news from other sites) but relatively few tools for building news feeds. The best one I found was FeedCreator. It can generate nine different formats:- ATOM 0.3
- HTML
- Javascript
- mbox
- opml
- PIE 0.1
- RSS 0.91
- RSS 1.0
- RSS 2.0
Simple to use
I found the FeedCreator class easy to use. It’s just one file. Include it, and go. Done. The code is documented, and contains enough examples to get you going. Don’t stop there. Read the code, or at least scan through it. You’ll find several things you may want to use. How easy is it? With one file, and a few symlinks, I can provide you with nine different formats of newsfeeds, all from the same source file. Actually, there’s two source files, but that’s not my point. If you look in the newsfeed directory, you’ll see links to the various formats. Each of those links actually points to a symlink. In that file, the code decides what format you want, and asks for it. It’s something like this:The first line determines the format you want, based upon the filename you selected (e.g. rss2.0.php). The second line invokes my newsfeed function and supplies the format you want. What does the function$format = basename($_SERVER['PHP_SELF'], '.php'); echo newsfeed($db, $format);
newsfeed
do? It’s pretty much the same example code you will find
with the FeedCreator class. I’ve customized it for my field names etc, but that’s about it.
One thing I did do slightly different, was force the FeedCreator caching mechanism to always use the
cache file, if it exists, regardless of how old it is. The cache files will be deleted by an external
mechanism, so FeedCreator does not need to refresh the cache if an appropriate entry is found. I’ve
told FeedCreator not to do this by supplying the following parameters to it:
A short explanation of the above parameters may help:$rss->useCached($Format, NEWSFEEDCACHE, time());
- $Format
- The format you want, based on the file name in the URL
- NEWSFEEDCACHE
- The name of the cache entry for this newsfeed. FeedCreator will use this file if it exists, and will create the newsfeed, save it to that file, and return the feed if it does not.
- time()
- This uses inside knowledge of the code and supplies the current timestamp. This ensures FeedCreator will always use an existing cache entry.
Useful Resources
I found the following resource useful when researching news feeds.- WikiPedia entry on ATOM
- WikiPedia entry for RSS
- Rss Standards (many links to RSS resources)