Share your Twitter & RSS subscribers with PHP

Many modern web designs are taking full advantage of social media integration by sharing their user base with their audience. In this tutorial we will look at some examples of websites which are implementing this effectively, as well as showing you how to create your own subscriber feed quickly and easily using PHP.
Subscriber feeds are generally, but not always RSS and Twitter. There are many reasons for wanting to display your subscriber and follower count, some of these can include:
- Giving confidence to new users who are viewing the website for the first time.
- Encouraging users to follow and/or subscribe themselves.
- Giving faith to potential clients or advertisers viewing the site.
Sites that do it well
There are a number of very successful websites that already share their subscriber counts to great success.
Lee Munroe’s blog uses an understated font and icon set to give precedence to his blog, whilst maintaining a controlled and professional tone.
One extra pixel is a great example of using imagery and colours to integrate the statistics into its website without feeling overworked.
Smashing magazine has pretty much been at the forefront in embracing social media and subscription stats on its site. in new and informative means.
web designer dev combines its subscribers to display an overview of the subscriptions. another a smart way of informing users without information overload.
Display your own subscriber stats
You can display your own subscriber stats quickly and easily with these simple PHP functions. Firstly you will need to know your feedburner ID, if you are not sure what this is or where to find it, why not read a my PHP feedburner function tutorial for detailed instructions.
Both functions displayed below make use of API’s which enable you to quickly and easily access publicly available information.
<?php
/**
* @name subscriber stats
* @author Philip Beel
* @description Display your feedburner and twitter stats anywhere on your page
**/
function feedburner_stats($feed, $attribute) {
if ( !$feed ) { echo('[No Feed]'); } //user must pass in their URI
if ( !$attribute ) { echo('[No Attribute]'); }//user must pass in their attribute
$xmlobj = simplexml_load_file("http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed);
if($xmlobj) { //check we got back a response
echo($xmlobj->feed->entry->attributes()->$attribute);
} else {
echo("0"); //else fall back on a static value
}
}
function twitter_stats($id) {
if ( !$id ) { echo('[No Feed]'); } //user must pass in their username
$xmltwitter = simplexml_load_file("http://twitter.com/statuses/user_timeline/".$id.".xml");
if($xmltwitter) { //check we got back a response
echo($xmltwitter->status->user->followers_count);
} else {
echo("0"); //else fall back on a static value
}
}
?>
As you can see the functions are not that dissimilar, I have broken them out into separate calls for the sake of this tutorial, but it would be simple to convert this into 1 single function call, with a set of params depending on what you wanted to pull out. I have added a simple catch to make sure something is returned, and in the eventuality that the API cannot be contacted, you can have a fallback, in this case 0, but you can change it to any static value you like.
When displaying the feed on your page, simply call the functions like so:
<ul>
<li><a href="http://feeds.feedburner.com/co/VazT" title="feed"><?php feedburner_stats('co/VazT', 'circulation'); ?></a><br /> Subscribers</li>
<li><a href="http://twitter.com/philipbeel" title="twitter"><?php twitter_stats('philipbeel'); ?></a><br /> Followers</li>
</ul>
This makes life much easier when you want to call only one value at a time, by simply making the function call where you want the stat to appear. Check out the full demo in action, or download the source code from the links below.
Im always looking to improve the code, so if you have any suggestions, or spot any bugs, please let me know. Happy coding!
















April 29th, 2010
Social comments and analytics for this post…
This post was mentioned on Twitter by elranoded: Share your Twitter & RSS subscribers with PHP http://su.pr/2DC1C5 #twitter #rss #php…
April 30th, 2010
Great article! I was looking for a way to share subscriber numbers just like this. I’ll be adding it to my site soon. Thanks!
« Reply
May 5th, 2010
How do I implement this code using a system based on SMARTY?
Tks
« Reply
philipbeel Reply:
May 5th, 2010 at 8:50 am
@Wagner, You would want to put the code into a smarty plugin, and add params to change what is being passed in, to your twitter username and your RSS id. Many Thanks
« Reply
May 24th, 2010
Thanks for the mention Philip, it’s a neat trick.
« Reply