---
title: "Configuring Facebook and Twitter Counters for your drupal websites"
description: "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…"
url: https://www.specbee.com/blogs/configuring-facebook-and-twitter-counters-your-drupal-websites
type: Article
author: "Kiran Kumar"
published: 2014-04-01
updated: 2026-07-31
---

# Configuring Facebook and Twitter Counters for your drupal websites

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.

![Image removed.](https://www.specbee.com/core/misc/icons/e32700/error.svg "This image has been removed. For security reasons, only images from the local domain are allowed.")

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'),

'cache' => DRUPAL\_CACHE\_GLOBAL,

'status' => 0,

'weight' => 0,

'visibility' => BLOCK\_VISIBILITY\_LISTED,

),

);

}

/\*\*

\* 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();

break;

} return $block;

}

function social\_status()

{ global $user;

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

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

$content .= '`<a href="www.twitter.com/yourpage" 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="www.facebook.com/yourpage" 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() {

get\_tweet\_count();

get\_fbcount();

}

Function to get Facebook page like count.

function get\_fbcount(){

$url = "www.graph\[dot\]facebook.com/yourpage";//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\_SSL\_VERIFYPEER, FALSE);

curl\_setopt($ch, CURLOPT\_HTTPHEADER, array('Host: graph\[dot\]facebook.com'));

$fbdata = curl\_exec($ch);

curl\_close($ch);

$obj = json\_decode($fbdata);

if($obj)

{

$followers\_count=$obj->likes;

}

if($followers\_count > 0){

variable\_set('social\_fb\_count',$followers\_count);

}

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 = 'api\[dot\]twitter.com';

$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 = "www\[dot\]//$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("&","&",$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\_HEADER => false,

CURLOPT\_URL => $url,

CURLOPT\_RETURNTRANSFER => true,

CURLOPT\_SSL\_VERIFYPEER => false);

// do our business

$feed = curl\_init();

curl\_setopt\_array($feed, $options);

$json = curl\_exec($feed);

curl\_close($feed);

$twitter\_data = json\_decode($json);

if($twitter\_data\[0\]->user->followers\_count >0){

variable\_set('uxm\_tweet\_count',$twitter\_data\[0\]->user->followers\_count);

}

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.

![Image removed.](https://www.specbee.com/core/misc/icons/e32700/error.svg "This image has been removed. For security reasons, only images from the local domain are allowed.")