Code: Subscriber Count
If you follow me on twitter, you might have saw me mention earlier today that I was working on a really nerdy blog post. In an effort to post a little more, I decided that I should share a simple PHP function I wrote today so that maybe someone can get some use out of it. You can use this function on your website to consolidate your follower counts into a single number that you can then style and display. Right now, it supports Feedburner (most blogs RSS feed provider. If you aren’t using it, I’d suggest it.), a Facebook Fan Page and a Twitter account. You can mix and match out of the three if you don’t use all three services.
Another thing worth mentioning is that when you’re pulling data from other servers, especially with how popular Facebook and Twitter are, it could potentially make your site stop rendering the page at the number while it’s waiting for the results. This function caches your follower count (which you can turn off) into a text file so that any page loads for the rest of the day won’t have to queue up all three additional servers.
If you are running a WordPress site, just pop the function into your themes functions.php file. The cache text file will be saved into your theme folder. If not, it will be saved to the root directory of your site.
Here’s the function:
////////////////////////////
/*
Function: Subscriber Count
$feedburner is the custom extension of the feedburner url. http://feeds2.feedburner.com/XXXXXXXXX
$facebook is the extension of your Facebook Fan page. http://www.facebook.com/XXXXXXXXX
$twitter is your Twitter username. http://www.twitter.com/XXXXXXXXX
*/
function subscriberCount( $feedburner, $twitter, $facebook, $cache = true ) {
$count = 0;
if( $cache == true ) :
$interval = 24 * 60 * 60; // 24 hr
if( function_exists( 'get_bloginfo' ) ) :
$cache_path = TEMPLATEPATH;
else :
$cache_path = $_SERVER['DOCUMENT_ROOT'];
endif;
$cache_path .= '/subscriber_count.txt';
if( file_exists( $cache_path ) && ( time() - $interval ) < filemtime( $cache_path ) ) :
$file = file_get_contents( $cache_path );
if( !empty( $file ) ) :
return number_format( strval( $file ) );
endif;
endif;
endif;
if( $feedburner ) :
$xml = simplexml_load_file('https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri='.$feedburner);
$count += strval( $xml->feed->entry['circulation'] );
endif;
if( $facebook ) :
$api_page = 'https://graph.facebook.com/' . $facebook;
$json = file_get_contents ( $api_page );
$profile = json_decode( $json );
$count += strval( $profile->fan_count );
endif;
if( $twitter ) :
$api_page = 'http://twitter.com/users/show/' . $twitter;
$xml = file_get_contents( $api_page );
$profile = new SimpleXMLElement( $xml );
$count += strval( $profile->followers_count );
endif;
if( $cache == true ) :
file_put_contents( $cache_path, $count);
endif;
return number_format( $count );
}
To use it, just run this function wherever you want the text to be displayed:
<p>I Have <?=subscriberCount('arkitectdesign', 'mattfelten', '')?> Subscribers!</p>
As you can see, I’m using my Feedburner account and my Twitter account. I don’t have a Facebook Fan Page so I left it blank.
Leave me a comment if you find this useful at all. I’d love to see if it was used somewhere.
Hi! Thanks for code, but I have error in error.log:
PHP Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=catswhocode"
Error on line in functions.php:
$xml = simplexml_load_file(‘https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=’.$feedburner);
And in txt-file count is 0.
Help, please.
Hey Dmitry,
I took a quick google into your situation. I’m either thinking your server is running on PHP4 (simplexml is a PHP5 function) or that there’s a php.ini setting, allow_url_fopen, that’s not on. That would make getting any external urls impossible.
I made a test case on my own server using your feedburner url and it worked fine. Here’s a screenshot:
http://cl.ly/60c66ef7604fd42962ce
I’d say it’s a server problem, not the functions. Hope this helps.
I have 5 subscribers on my feedburner acount. If i use FeedCount i get 5, but if I use the API I get 0.
You know why?
Thanks
You might want to make sure the Awareness API is activated on your account. See my link below for more information. If that doesn’t solve it, I’d imagine it has to do with Google/Feedburner caching. I hear they have a lot of problems with subscriber count staying accurate.
http://code.google.com/apis/feedburner/developers_guide.html#activate
hate it when people post stuff with bare instructions to seem cool.
We Have Fatal error: Call to undefined function subscriberCount() in wp-content\themes\c2\footer.php on line 76
I’m sure this works but not enough instruction. Will find alternate on another blog.
Hey James,
I feel it is actually quite self-explainatory to anyone who knows a little bit of PHP. If you don’t know PHP, you probably shouldn’t be looking up PHP functions. If you have any questions, feel free to ask, otherwise, good luck in your search.