Retrieve, Cache, and Display Your FeedBurner Subscriber Count

By  on  

One way of significantly speeding up your website is by caching information you retrieve remotely from other websites or databases. Caching information is extremely simple and can save your users seconds of unnecessary waiting. Here's how I cache my FeedBurner follower count on my site.

The PHP

/* settings */
$cache_path = '/cache/';
$rss_file_name = date('Y-m-d').'-rss.txt';

/* rss */
if(file_exists($cache_path.$rss_file_name)) {
	$rss_subcribers = file_get_contents($cache_path.$rss_file_name);
}
else {
	$rss_content = get_url('https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=dfadajf324789fhSDFDf48');
	$subscribers = get_match('/circulation="(.*)"/isU',$rss_content);
	if($subscribers) {
		$subscribers = number_format($subscribers,0,'',',');
		file_put_contents($cache_path.$rss_file_name,$subscribers);
		$rss_subcribers = $subscribers;
	}
}

/* display */
echo 'My subscriber count is: ',$rss_subscribers;

/* connects to the URL, returns content */
function get_url($url) {
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
	$content = curl_exec($ch);
	curl_close($ch);
	return $content;
}

/* helper: does the regex */
function get_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}

The first thing we do is check to see if our cached file exists. If it does, we simply return its contents. If the cache file doesn't exist, we connect to our private FeedBurner URL, parse the content, and save the feed to a file named after the current day. Note the way we can check if we had gotten the count for the current day is by naming the file in "year-month-day" format.

Recent Features

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

Incredible Demos

  • By
    Comment Preview Using MooTools

    Comment previewing is an awesome addition to any blog. I've seen really simple comment previewing and some really complex comment previewing. The following is a tutorial on creating very basic comment previewing using MooTools. The XHTML You can set up your XHTML any way you'd like.

  • By
    MooTools Zebra Table Plugin

    I released my first MooTools class over a year ago. It was a really minimalistic approach to zebra tables and a great first class to write. I took some time to update and improve the class. The XHTML You may have as many tables as...

Discussion

  1. It may be only a small concern (and an “undocumented feature” for some users), but this function results in a new text file being created for every day it is triggered. Whilst alot of webhosts give very generous file storage quotas, and a feed’s popularity could be determined over time using these files, it seems a little wasteful.

    Would another option be to have a single text file (“feedburner_sub_count.txt”) and save into that file a serialized array containing two key-value sets keyed as “date” and “count”?

    Of course, this would mean that the file would have to be loaded and unserialized to get the “date” value and then figure out whether it needed to pull data from Feedburner again (to replace the array values and then serialize and resave the new array in the same file), but it would mean only one file.

  2. I hear you Luke. I prefer separate files for the sake of simplicity but your idea is a good one for those that would like to stick to one file.

  3. I know not everyone can do this, but I actually run a cron job on my server that replaces the feed count every 2 hours or so. The difference there is I make a direct replacement in my wordpress theme instead of using a text file.

  4. Nathan barnett

    It makes sense to me to use a simple library like Cache_Lite from PEAR to handle this sort of problem. The nice thing about using Cache_Lite is that you can set an expiration time and have the natural flow of traffic decide when to repopulate the cache. It can also be automatically serialized if your saving arrays and the like. In a high traffic environment its often important to set the cache expiration to never expire and re-populate the cache via cron. I personally use a simple wrapper to use either Cache_Lite or memcached so I can easily switch between the two caching methods depending on what works best for the specific problem.

    http://pear.php.net/package/Cache_Lite
    http://us3.php.net/memcache

  5. Memcache > cache files

  6. @Luke: Instead of storing the date inside the file, why not just use the time when the file was modified?

    $interval = 24 * 60 * 60;  // 24 hr * 60 min / hr * 60 sec / min
    if( file_exists( $path_to_file ) && ( time() - $interval ) < filemtime( $path_to_file ) ) {
         readfile( $path_to_file );
         exit();
    }
    
    // find new information and store it in the file
    
  7. Luke

    @Karthik: Even better! Hadn’t thought of that option myself, but, I must say, a very elegant solution.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!