Facebook Twitter and Google Plus Fan Counts PHP

The Facebook, Twitter and Google plus data are available publicly in Json format, we can use some information to easily display number of likes, followers and circle counts in plain text. I wanted to include Feedburner Subscriber count code too, but too bad Google has decided to put an end to Awareness API, without it we can not display subscriber count in plain text.

The URLs in box below are three magic URLs from these top sites, which contains information about people and pages in JSON format.
 
1
2
3
http://api.twitter.com/1/users/show.json?screen_name=<screen NAME>;
http://graph.facebook.com/<page ID OR NAME>;
https://www.googleapis.com/plus/v1/people/<page ID>?key=<google API KEY>;
If you execute these URLs in browser address bar with correct variables, it should return some data about respective page or person. For example, this Facebook graph URL https://graph.facebook.com/saarraan returns information about saaraan’s Facebook Page.
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
   "name": "Saaraan",
   "is_published": true,
   "website": "http://www.sanwebe.com/",
   "username": "saarraan",
   "description": "Site offers Web Development tips & freebies.",
   "about": "FB home u2665 http://www.saaraan.com ||  Twitter u2665 https://twitter.com/saaraan",
   "talking_about_count": 39,
   "category": "Computers/internet website",
   "id": "261418177232851",
   "link": "https://www.facebook.com/saarraan",
   "likes": 563,
   "cover": {
      "cover_id": 458954497479217,
      "source": "http://m.ak.fbcdn.net/sphotos-e.ak/hphotos-ak-ash3/s720x720/575192_458954497479217_864306488_n.png",
      "offset_y": 0
   }
}
Notice the likes property of the Json object? similarly Twitter has followers_count,
and Google Plus has plusOneCount property. All we need is a mechanism to retrieve these values and use it in our projects. For that I have created a small PHP function which uses cURL or file_get_contents to fetch the data, it will then decode and parse JSON data in PHP objects, which we will use to display counts in our pages.
Have a look at complete code below:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

########### settings #########$Twitter_Screen_Name        = 'sanwebe'; //Twitter screen name
$Facebook_Page_ID_Or_Name   = 'sanwebe'; // Facebook Page ID or Name
$Google_Page_Id             = '108054684530298582648'; // Google Page ID
$Google_API_key             = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; //Google API key
##############################
$twitter_link = 'http://api.twitter.com/1/users/show.json?screen_name='.$Twitter_Screen_Name;
$facebook_like = 'http://graph.facebook.com/'.$Facebook_Page_ID_Or_Name;
$google_page_circle = 'https://www.googleapis.com/plus/v1/people/'.$Google_Page_Id.'?key='.$Google_API_key.'';

$twitter_data       = get_data($twitter_link);
$facebook_data      = get_data($facebook_like);
$google_data        = get_data($google_page_circle);

echo 'Twitter Followers  : '. $twitter_data->followers_count .'<br />';
echo 'Facebook Fans      : '. $facebook_data->likes.'<br />';
echo 'Google Page Circle : '. $google_data->plusOneCount;

echo '<pre>';
//print_r($twitter_data);
//print_r($facebook_data);
//print_r($google_data);
echo '</pre>';


function get_data($json_url='',$use_curl=false)
{
    if($use_curl)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $json_url);
        $json_data = curl_exec($ch);
        curl_close($ch);
        return json_decode($json_data);
    }
    else
    {
        $json_data = file_get_contents($json_url);
        return json_decode($json_data);
    }
}
?>

Returns

Twitter Followers : 1525
Facebook Fans : 1100
Google Page Circle : 19

No comments:

Post a Comment