Building a Social Profile Widget: Get Your Follower Count

A feature that's becoming more common with websites is the social media profile widget. The widget consists of icons and text that link to a number of social network profiles to which the website is associated. Some of these widgets also show the number of followers on each of their respective social network.

There are a lot of free and premium social profile widget plugins available for WordPress that one can use in linking to his/her personal or website social network's profiles.

In this two-part series, I will show how to get the count of Facebook page Likes, Twitter and Google+ Followers, and how to build a social profile WordPress widget.

We'll be focusing on Facebook, Twitter, and Google+ because they are three of the biggest social networks.

Facebook Likes Count

The Facebook Graph API can be used in a variety of ways to communicate and access Facebook data such as retrieving profile information of users, posting new stories, upload photos, retrieve posts, and a number of other tasks right inside an application.

Using the Graph API, we can programmatically retrieve the number of Likes of a Facebook page. For example, to retrieve the Date founded, About, Name and Likes count of Tuts+ Facebook page, point your browser to http://graph.facebook.com/tutsplus to reveal JSON data containing all the information about the page.

Note we are only interested in the page's number of Likes (which is the value of the object property "likes") with respect to the social profile widget we will be building in the second part of this article.

Below is a PHP function that returns the number of Likes of a Facebook page.
This function accepts a Facebook page's username as its argument and returns the number of Likes.

Say we want to get the get the page Likes count of Tuts+ Facebook page, pass the username tutsplus to the function like this:

Google+ Follower Count

Before you can use Google's API to retrieve information about a Google+ profile or page, you need to obtain a developer API key. Follow the steps outlined below to generate your own API key.

  1. Login to Google Developers Console using your Google's account, click on the Create Project button and fill the form to create a project.
    Google Developer Console
  2. Click the newly created project to go to its dashboard, navigate to the API sub-menu under APIs & auth and activate the Google+ API.
    Google API
  3. To get the API key, navigate to the Credentials page, click the Create new Key and proceed to creating a Server key
    Create a new key
  4. Your API key should now be displayed as depicted in the image below.
Public API Access

The PHP function below retrieves and return the follower count of a Google+ profile or page.

To use the function, pass a Google+ profile username (with the leading + symbol) or an ID and your API key to the function.

For example, my Google+ user name is +agbonghamacollins and my ID is 116181276412462774298.

Passing either my Google+ username or ID together with my API key to the function as argument will return the number of people following me on G+.

Twitter Followers Count

Twitter uses OAuth which is an open protocol to allow secure authorization in a simple and standard method that allows users to approve application to act on their behalf without sharing their password.

To successfully send requests to the Twitter API, you need to create an application with OAuth authorization since unauthorized requests aren't allowed.

To create a Twitter application, you need to login to the Twitter developer dashboard using your Twitter account. The point of creating an application is to give yourself (and Twitter) a set of keys. These are:

  • The consumer key
  • The consumer secret
  • The access token
  • The access token secret

These keys will come in handy when we are to query the API for followers count.

Follow the steps below to create a Twitter application and to generate the keys.

  • Login to Twitter developer account using your Twitter account and navigate to the Application Management console.
  • Click on the Create New App button to initiate the Twitter application creation.
  • Fill the form and click the submit button to create the application.
  • Click on the application, navigate to the Permissions tab and change the Access level to Read and Write.
    If you want to make any decent use of this API, you'll need to change your settings to Read & Write if you're doing anything other than standard data retrieval using GET requests.
    Twitter Demo App

To get your application consumer key and secret, navigate to the API Keys tab.
The API key and API secret is the consumer key and consumer secret, respectively.

Twitter Demo App Settings

To get the application access token and access token secrets, still in the API Keys tab, scroll downward and click on the Create my access token button to create the access tokens.

Access Tokens

Refresh the page and your application Access tokens will be shown to you.

Your Access Token

We now have the consumer key and consumer secret as well as the access token and secret keys. Next, we need to programmatically retrieve our number of Twitter followers.

I found a very useful Simple PHP Wrapper for Twitter API that make sending request and receiving response from the API painless.

To use the Twitter PHP wrapper, download and install it via Composer simply by adding the following require statement to your composer.json file:


Run $ php composer.phar install to download the library and generate the vendor/autoload.php autoloader file.

Include the vendor/autoload.php or alternatively, download the PHP wrapper in zip archive, extract and include the TwitterAPIExchange.php file which contain the wrapper class.

The function below will retrieve the count of Twitter followers of a user. To use the function, set your Twitter application's consumer key, consumer secret, access token, and access token secret.


To use function, pass the twitter handle or username to the function as its argument.

Summary

In this first part of the series, we've created three PHP functions that handle the retrieval of Facebook page likes, Google+ fans, and Twitter followers.

In the second and last part of this series, we will be building the Social Profile WordPress widget that links to a Facebook, twitter and Google+ profiles and also display the followers count as depicted in the image below.

Be sure to check out the next part of the series for further implementation details. In the meantime, I welcome any and all feedback in the comments.

Tags:

Comments

Related Articles