Post Pic

How to display your average feed readers

Many bloggers are using Feedburner “chicklet” to display their subscribers count. If you want to be able to display the count for an average 7 days, here is a piece of code you’re going to love.

As usual, the first thing to do is to paste the function in your functions.php file:

function get_average_readers($feed_id,$interval = 7){
	$today = date('Y-m-d', strtotime("now"));
	$ago = date('Y-m-d', strtotime("-".$interval." days"));
	$feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $feed_url);
	$data = curl_exec($ch);
	curl_close($ch);
	$xml = new SimpleXMLElement($data);
	$fb = $xml->feed->entry['circulation'];

	$nb = 0;
	foreach($xml->feed->children() as $circ){
		$nb += $circ['circulation'];
	}

	return round($nb/$interval);
}

Once done, you can call the function wherever you want in your theme files. Pass your Feedburner feed id as a parameter:

<?php
$nb = get_average_readers('catswhocode');
echo "I have ".$nb." RSS readers";
?>

Code initially published on Cats Who Blog.

5 Responses

Jul 23 2010 14:15

Gotta put some caching in there or it’ll kill your blog..

Aug 06 2010 04:35

I am getting following error:

Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’

on this line “$xml = new SimpleXMLElement($data);”

Aug 08 2010 11:52

@Jitendra Kumar Probably data you are passing to SimpleXMLElement is not a XML at all. Make sure that in FeedBurner options you made your statistics available to the public (sorry do not remember where and how it’s named exactly).

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required