Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pulls the Facebook fan count and Twitter follower count for the specified accounts.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var f_page = "wvumountaineers"; // the page name for your fan page, e.g. the 'wvumountaineers' part of http://facebook.com/wvumountaineers
var t_page = "westvirginiau"; // the account name for your main twitter account
function add_commas(number) {
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0)) {
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
} else {
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
}
return (output);
} else {
return number;
}
}
// when document is ready load the counts
$(document).ready(function(){
// grab from facebook
$.getJSON('https://graph.facebook.com/'+f_page+'?callback=?', function(data) {
var fb_count = data['likes'].toString();
fb_count = add_commas(fb_count);
$('#fb_count').html(fb_count);
});
// grab from twitter
$.getJSON('http://api.twitter.com/1/users/show.json?screen_name='+t_page+'&callback=?', function(data) {
twit_count = data['followers_count'].toString();
twit_count = add_commas(twit_count);
$('#twitter_count').html(twit_count);
});
});
</script>
</head>
<body>
<noscript>The following counts are dynamically populated by JavaScript. You can also directly visit the sources to find the counts at http://facebook.com/[pagename] and http://twitter.com/[accountname]</noscript>
Facebook fan count: <span id="fb_count"></span><br />
Twitter follower count: <span id="twitter_count"></span><br />
<br />
<em>You could put these spans in nice little badges or something...</em>
</body>
</html>
@sethadam1

This comment has been minimized.

sethadam1 commented Nov 15, 2010

I rewrote this in PHP to avoid the javascript dependency.

http://dev.firsttube.com/fans-followers.php

@dmolsen

This comment has been minimized.

Owner

dmolsen commented Nov 16, 2010

@sethadam1 thanks for letting me know :) I've also updated my blog post to link to your PHP code as well.

@yashmistrey

This comment has been minimized.

yashmistrey commented Nov 6, 2011

but facebook count is showing 0 ?

@dmolsen

This comment has been minimized.

Owner

dmolsen commented Nov 6, 2011

@yashmistrey hrm, i just tested the gist as it is above and it works. what facebook page are you targeting?

@logeshpaul

This comment has been minimized.

logeshpaul commented Jul 22, 2012

Awesome! It works perfectly. Thanks for the code

@joshmoto

This comment has been minimized.

joshmoto commented Aug 3, 2012

See I find I prefer the javascript version cause you can fire it onload - where as the php version is great, but if there any lag on twitter or facebook, your site experiences it.

@vinniejames

This comment has been minimized.

vinniejames commented Aug 21, 2013

Any one able to get the twitter count working with API 1.1 ?

@x85301

This comment has been minimized.

x85301 commented Nov 19, 2013

Twitter count shows nothing?

@irfan16

This comment has been minimized.

irfan16 commented Nov 23, 2013

hey hi,
Thanks for great post,
but twitter followers can't show i use API 1.1 but there is nothing any one help?
THankx

@arifsclick

This comment has been minimized.

arifsclick commented Nov 29, 2013

I like this script. As a web developer I have done a like checker website. I am using that there.

@ghost

This comment has been minimized.

ghost commented Feb 1, 2014

Twitter for the script does not work, please fix it

@Zeokat

This comment has been minimized.

Zeokat commented Mar 9, 2014

Zeokat thanks for the pirce of code, but as paulvilla said, Twitter part not works.

@ghost

This comment has been minimized.

ghost commented Jun 17, 2014

This technique won't work with how Twitter has updated their API. You have to use OAuth or get crafty with a proxy.

@enkhee

This comment has been minimized.

enkhee commented Jun 23, 2014

Twitter API update please .. Don't work twitter

@Kingturo

This comment has been minimized.

Kingturo commented Feb 11, 2016

Not working, also getting error on toString method

@CHANDRA-BHUSHAN-SAH

This comment has been minimized.

CHANDRA-BHUSHAN-SAH commented Aug 24, 2016

Is there any way to get Facebook fan count with secure request i.e. without "https" request as you made in twitter request that starts with http not https?

@shameermohamed

This comment has been minimized.

shameermohamed commented Nov 21, 2016

the script is not working for me. Showing the count as 0 always.

@v0ff4k

This comment has been minimized.

v0ff4k commented Jul 15, 2017

both of this social network's API is updated. So this code is outdated !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment