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
- PIE 0.1
- RSS 0.91
- RSS 1.0
- RSS 2.0
The only format missing that I wanted was ATOM 1.0, but there is no reason why I can't add that to
the class myself.
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:
$format = basename($_SERVER['PHP_SELF'], '.php');
echo newsfeed($db, $format);
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
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:
$rss->useCached($Format, NEWSFEEDCACHE, time());
A short explanation of the above parameters may help:
- The format you want, based on the file name in the URL
- 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.
- This uses inside knowledge of the code and supplies the current timestamp. This ensures
FeedCreator will always use an existing cache entry.
The main reason I wanted to use a class to do these feeds is simplicity. I now have one piece of code
that generates all the formats. It's the same code, so now if there's a bug with the news feed, and it's not
formatting, there is just one place to fix, not nine. It also means that if I want to add a new format,
such as ATOM 2.0, I can extend FeedCreator, create the new symlink, and I have a new feed format. I don't have
to touch the code that pulls data from the database (unless of course I'm adding additional information to
I am now using FeedCreator on both this website and FreshPorts. So far,
I'm very pleased with being able to offer additional services to you without having to spend several days on coding.
My thanks to the FeedCreator team.