It’s hard to discover how to write a dynamic RSS feed with php, since so many resources on the web strictly deal with parsing or consuming RSS-style feeds, and relatively few discuss how to provide them. Dynamic RSS feeds are special because they are dynamically generated by a script on the fly, depending on the query parameters that you pass along.
Most resources take the perspective of static RSS, but dynamic RSS has certainly gained a stronger following as more and more sites implement it. Some major sites that do so include craigslist.org (my favorite is putting an RSS feed on ‘Porsche 911′ within the “For Sale” section
) and the venerable eatlunch.at.
My first stab at an implementation creates the XML by a bunch of carefully constructed print statements, which works fine because most XML creation modules need to build the entire tree in memory before rendering, which gets memory intensive. Printing the whole thing out line by line is a pretty aggressive optimization, but it’s worth it.
The first problem I encountered is Bloglines never fetching updates. That’s clearly not good, but how to solve it?
It turns out that Bloglines makes use of the optional Syndication elements of your feed. The Syndication elements are hints to feed readers that tell them how often to check the feed.
To add them to your feed, first add the namespace to the rss / rdf root xml element like so:
Then, you’ll add in the following subelements of the channel element:
Together, these elements will provide feed readers with clues. I’m not sure if Bloglines requires these, but since dynamic RSS is provided via URIs whose timestamps don’t change along with their content, there’s no other way that Bloglines can set up its internal caching mechanism.
So, I hope this helps you create quality RSS feeds as well!
I’m using Feedcreator:
http://www.bitfolge.de/rsscreator-en.html
and did not experience the bloglines problem. What I did notice is that the Bloglines update rate is heavily dependent on the popularity of the feed (= whether you’re the only subscriber or not).
You might also take a look at Simon’s entry on support conditional GET on dynamic feeds:
http://simon.incutio.com/archive/2003/04/23/conditionalGet
I made an dynamic rss feed with ASP and SQL, and the page is an asp page, with the extention .asp. I have noticed that a lot rss readers don’t read it because of the .asp extention. do you run into the same problem using php?
Mark – I typically use mod_rewrite on Apache in order to mask the actual file being used to generate the feed. I’m not sure if you’re running IIS or how you might go about doing that with anything other than Apache.
If you’re running IIS with PHP, you can make a script that rewrites (or deletes, then writes) the XML/RSS document based on a pull from the server. This can be manual by making it a link you select that performs the rewrite functionlity, or it can be dynamic by scripting it into your CMS so when something is published the code updates the XML/RSS.