Show FeedBurner Circulation using PHP

As it turns out, I'm not a fan of the Feedburner chicklet (as they call it) that gives you your total subscriber list. In fact, what if you wanted something a little more unique?

Borrowing heavily from this code example, I put together this quick script that pulls in your total circulation:

<?
// define parameters
$feedid = 'snookca';
$cache_time = '43200'; // in seconds (43200 = 24 hrs)
$feedburner_cache = 'feedburner_cache.txt' ;

// Make sure we cache data, so that we do not have to contact
// Feedburner for every request.
if (!file_exists($feedburner_cache) || (time() - $cache_time > filemtime($feedburner_cache))) {
    $tmp = file_get_contents('https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=' . $feedid) ;
    $fp = fopen($feedburner_cache,'w') ;
    fputs($fp, $tmp) ;
    fclose($fp) ;
}

// pull in data from cache
// $fp = fsockopen ($host, '80'); // (this also works)
$fp = fopen($feedburner_cache,'r') ;
if ($fp) {
	while (!feof($fp) ) {
	    $xml .= fgets($fp,1024) ;
	}
	preg_match("/circulation=\"(\d+)\"/i", $xml, $circulation);
	echo '(' . $circulation[1] . ')';
	fclose($fp) ;
}else{
	// fail silently
}
?>

To use this code, just replace snookca with your site id. What this does is reads in the xml response from the FeedBurner API (which must be turned on for your feed if you haven't done so already) and writes it to a file. Any subsequent request will read from the cache instead of hitting the web service again. Then, the script reads the file from the cache and does a regex to match the circulation="XX" part in the XML.

To see the code in action (at time of writing), check out the home page and look in the footer next to the link to the Technorati profile. That number in brackets is my circulation (currently zero... go figure).

Check out the FeedBurner Awareness API for more information.

Published August 29, 2005 · Updated February 24, 2009
Categorized as PHP
Short URL: https://snook.ca/s/406

Conversation

6 Comments · RSS feed
Jeff Adams said on August 30, 2005

(1058) go figure.

This will come in very useful, Thanks

o-juice said on September 05, 2005

Thanks! :-D

jim said on September 06, 2005

Newbie here - I can't just copy and paste this into my sidebar, eh?

If you have a minute or two, could you let me know how to use this?

Thanks!

Jonathan Snook said on September 07, 2005

jim: no, not really. To use this code, it's best to have a little knowledge of your hosting environment. If you're using WordPress then, yes, it should be fairly easy to integrate. Anything else might be a little trickier.

For example, i use MovableType but was originally using static templates. I had to convert everything over to PHP in order to integrate this script (among others I've been looking to add).

If you have a little more information about your environment, I can see what I can do.

Panayotis Vryonis said on October 26, 2005

Thanks for mentioning the original code Jonathan! I did some work on the FeedBurner Management API too, you can find details at http://vrypan.net/feedburner_apis/.

Erde said on April 20, 2006

Looks very good - thanks!

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.