Configuring Facebook and Twitter Counters for your drupal websites

Apr 01,2014/Posted by Kiran Kumar

Social scientists use social statistics for many purposes, including:

  • the evaluation of the quality of services available to a group or organization,
  • analyzing behaviors of groups of people in their environment and special situations,
  • determining the wants of people through statistical sampling.

Create a Drupal custom block with number of Facebook page likes and total number of twitter followers of your website.

drupal websites

To begin with, create a custom module, for more information on how to create custom drupal modules please visit this page.

We will see how to create a block with counters for total Facebook page likes and twitter followers.

The example below shows how to create a social media counter block. In your custom_module place below code.


* Implements hook_block_info().


function custom_module_block_info() {

return array(

'social_stats' => array(

'info' => t('Social Media Counters'),


'status' => 0,

'weight' => 0,






* Implements hook_block_view().


function custom_module_block_view($delta = '') {

$block = array();

global $user;

switch ($delta) {

case 'social_stats':

$block['subject'] = t('Social Media Counters');

$block['content'] = social_status();


} return $block;


function social_status()

{ global $user;

$content = '<div class="follows">';

$content .= '<div class="row-1">';

$content .= '<a href="" target="_blank" class="inactiveLink"><div class="twit"><span class="counter">'.variable_get('social_tweet_count','24418').'</span><div class ="counter-text"><br/> followers</div></div></a>';

$content .= '</div>';

$content .= '<div class="row-2">';

$content .= '<a href="" target="_blank" class="inactiveLink"><div class="fb"><span class="counter">'.variable_get(' social_fb_count','3560').'</span><div class ="counter-text"><br/> page likes</div></div></a>';

$content .= '</div>';

$content .= '</div>';

return $content;


Write a hook_cron function to update your counters for every cron run.

/**cron hook function */

function uxm_cron() {




Function to get Facebook page like count.

function get_fbcount(){

$url = "";//Enter your facebook page url here.

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);


curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host:'));

$fbdata = curl_exec($ch);


$obj = json_decode($fbdata);





if($followers_count > 0){



return $followers_count;


Function to get Twitter followers count.

function get_tweet_count(){

$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //Enter your twitter token

$token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //enter secret_code

$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Enter twitter consumer key

$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Enter consumer secret

$host = '';

$method = 'GET';

$path = '/1.1/statuses/user_timeline.json'; // api call path

$query = array( // query parameters

'screen_name' => 'yourpage', // enter your screen name here.

'count' => '5'


$oauth = array(

'oauth_consumer_key' => $consumer_key,

'oauth_token' => $token,

'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended

'oauth_timestamp' => time(),

'oauth_signature_method' => 'HMAC-SHA1',

'oauth_version' => '1.0'


$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting

$query = array_map("rawurlencode", $query);

$arr = array_merge($oauth, $query); // combine the values THEN sort

asort($arr); // secondary sort (value)

ksort($arr); // primary sort (key)

// http_build_query automatically encodes, but our parameters

// are already encoded, and must be by this point, so we undo

// the encoding step

$querystring = urldecode(http_build_query($arr, '', '&'));

$url = "https://$host$path";

// mash everything together for the text to hash

$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);

// same with the key

$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

// generate the hash

$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

// this time we're using a normal GET query, and we're only encoding the query params

// (without the oauth params)

$url .= "?".http_build_query($query);

$url=str_replace("&amp;","&",$url); //Patch by @Frewuill

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!

ksort($oauth); // probably not necessary, but twitter's demo does it

// also not necessary, but twitter's demo does this too

function add_quotes($str) { return '"'.$str.'"'; }

$oauth = array_map("add_quotes", $oauth);

// this is the full value of the Authorization line

$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

// if you're doing post, you need to skip the GET building above

// and instead supply query parameters to CURLOPT_POSTFIELDS

$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),


CURLOPT_URL => $url,



// do our business

$feed = curl_init();

curl_setopt_array($feed, $options);

$json = curl_exec($feed);


$twitter_data = json_decode($json);

if($twitter_data[0]->user->followers_count >0){



return $twitter_data[0]->user->followers_count;


Save your module file. Flush drupal cache. Goto /admin/structure/block there you will find your custom block which you have created above. Place your block in desired page regions.

You can customize and style the counters by adding CSS and icon images to make it look beautiful.

drupal websites

Subscribe For Our Newsletter And Stay Updated

Recent blogs

As the internet continues to grow, more people around the world are getting familiar with it, and this means just one thing. If you...
b2all marketers
Energized by tech innovations, individuals today have very high expectations about what makes a great customer experience and fail...
In iOS 12, Apple introduced ARKit 2.0 with some extreme AR features like Multiuser AR experience, 2D image detection and 3D object...

This website uses cookies to offer you an enhanced website experience. We collect information about how you interact with our website to provide personalized services to you. To find out more, see our Privacy Policy