RSS Cron Job

If you are on shared web host, you might not have the ability to run lots of cron jobs or be limited to a certain number per hour or day. You might know about the “Poor Man’s Cron Job”, which is basically not to run a task (usually caching some data or fetching a feed, api, etc.) in the background until someone visits a page. Which isn’t ideal, because often the page will be slow or sometimes it won’t be up to date until the second visit (if you run that task after outputting the cached data).

Well one way to get around this is to setup an RSS feed for the data you are caching or outputting and access it with a query string such as “?rss=2.0” or http://example.com/index.php?rss=2.0. Then you can check to see if the RSS variable was passed and output a RSS feed with just enough data for a feed, no need to put sensitive data in there or anything. I would suggest putting in a ttl node in the RSS feed and set it to something the aggregators like Google Reader, should obey (that way they hit your page more or less frequently, depending on your needs). The item portion of an RSS feed only needs a title or a description, although its probably a good idea to put some kind of guid in there (check the RSS 2.0 spec for more info on creating RSS feeds).

Anyway here is some sample code. It isn’t complete but gives you an idea what I mean.

if ($_GET['rss'] == 2.0)
{
//create RSS 2.0 feed
header('Content-Type: text/xml');
$output = '<' . '?xml version="1.0"?' . '>' . "\n";
$output .= '<rss version="2.0">' . "\n";
$output .= '<channel>' . "\n";
//...
//process your data and output it into RSS 2.0 format
//...
$output .= '</channel>' . "\n";
$output .= '</rss>';
}
else
{
//process your data as normal
}

Then to make sure the page is hit often, place your RSS feed into Google Reader or Bloglines or some other RSS aggregator. Then your site will be visited often and forced to update. No one else really needs to know about your RSS feeds, unless you want them to be public as well.

Tags: ,

Comments -49-0 of 3 to “RSS Cron Job”

  1. -49 · Joel de Bruijn says:

    Came up with the idea myself, googled it, find this post and now I’m sure. My Use Case:
    Aggregated feeds from various social networks, about my activities, are imported as a lifestream in to a WordPress installation. Normally, I mis items if those feeds contain just 10 items, and my lifestream doesn’t updates after, for example, 15 items.
    Now, I import those feeds also in Google Reader. I always have it open…
    So thanks!

  2. -48 · Joel de Bruijn says:

    Don’t know if this stays possible now Google Reader gets the + treatment….

  3. -47 · blogger says:

    It will still work. All this relies on Google Reader to do is request the RSS feeds you create periodically (supposedly every hour).

Leave a Comment

Comments are reviewed before publishing to prevent spam.