Caching Feedburner subscriber statistics for blog or website

Cache Feedburner statistics for better performance and flexibilityMany of the big online Kahunas like Smashingmagazine and Mashable are displaying how many subscribers their RSS feed are having.

Displaying the number of feed subscribers can be a good idea from a marketing perspective since many subscribers to some users  equals that the site has big credibility.

Feedburner is one of the most popular RSS tools, and Feedburner also offers offers a widget that can be used to display the number of subscribers to your RSS feed. However since the Feedburner widgets uses javascript to fetch feed statistics from Feedburner, these widgets can potentially slow down your site.

In this post you’ll learn how to use the Feedburner API and PHP to fetch feed subscription data, and display these data on your site or blog in a efficient way

Getting Feedburner data

Feedburner subscription statistics are usually displayed on websites using widgets (Feedburner chicklets).

Feedburner widgets displaying active subscribers

To display the Feedburner widget, you need to embed the following HTML in your website or blog:


<p>
<a href="http://feeds.feedburner.com/tips4php/WMgM">
<img src="http://feeds.feedburner.com/~fc/tips4php/WMgM?bg=3300FF&amp;fg=FFFFFF&amp;anim=1" height="26" width="88" style="border:0" alt="" />
</a>
</p>

This solution makes it very easy to display Feedburner statistics, but is also has some limitations that I don’t like:

  • It’s difficult to control the look-and-feel. You can only customize body and text color
  • The external image in the counter, means an additional external HTTP request when the page is loaded. This can potentially slow down your site, even if Feedburner normally has decent response times

The good news is that Feedburner also has a API, that  can be used to get more flexibility and better performance.

Using the Feedburner API

Feedburner has a API that provides statistics about each feed.

In this post we’ll build a script with the following features:

  • Extracts feed statistics using the Feedburner API
  • Caches Feedburner response in a local file to improve performance

The script uses the PHP CURL function to request data from Feedburner.

To customize the script to your site you should insert/change the following variables in the code below:

  • $cache age. This value defines how old the cache file should be in seconds before new data is downloaded form Feedburner. “3600” equals 1 hour. Normally you should only renew the file every 6 hours.
  • $cache_file_path. The absolute file path, including file name to your cache file. My experience is that you should create a empty file, at the specified destination to get the script working. Eg: “/home/virtual/mydomain.com/var/www/html/cache.txt”
  • $rss_url. The URL of your Feedburner feed.
<?php
$cache_age = '5';
$cache_file_path = '<insert cache file url here>';
$rss_url ='<insert feedbruner feed url here>';

$cache = FALSE; //Assume the cache is empty
if(file_exists($cache_file_path)) {
 $cachetime = filemtime($cache_file_path);
 $timeago = time() - $cache_age;
 if($cachetime < $timeago) {
 $cache = FALSE; //Set to false just in case as the cache needs to be renewed
 } else {
 $cache = TRUE; //The cache still ok.
 }
}

if($cache === FALSE) {
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $rss_url);
 curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 $content = curl_exec($ch);
 curl_close($ch);

 if($content === FALSE) {
 echo ("Error fetching feed");
 }

 //Save data into the cache
 $fp = fopen($cache_file_path, 'w');
 fwrite($fp , $content);
 fclose($fp);
 $source = 'api';

} else {
 //Load data from the cache.
 $content = file_get_contents($cache_file_path);
 $source = 'cache';
}

$xml = new SimpleXMLElement($content);
$no_of_readers = $xml->feed->entry['circulation'];

echo $no_of_readers." Readers, $source";

?>

To make debugging easy, the example script has $cache_age=5, so data will be collected from Feedburner if the cache file is older than 5 seconds. I have also added the $source variable, that displays if the content is from the API or from the cache file.

Both things should be changed before going live with this script.

See example script working here

Conclusion

The script in this post extracts Feedburner statistics and uses local file cache to optimize performance.

The script delivers the subscriber information in a PHP variable, that easily can be formatted in any way you like.

The script is universally applicable in any PHP installation that has CURL function available. This means that the script can be used at both own developed PHP sites as well ans CMS and blogging platforms like WordPress and Typo3

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

One Reply to Caching Feedburner subscriber statistics for blog or website

  1. Great tips, I have been looking for something like this to complement my work out. Thanks!

Comments are now closed for this article.