Facebook Twitter and Google Plus Fan Counts PHP
Written by Saran on
November 20, 2012,
Updated
December 21, 2013The 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. [cc lang="html"] http://api.twitter.com/1/users/show.json?screen_name=
; http://graph.facebook.com/; https://www.googleapis.com/plus/v1/people/?key=; [/cc]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.[cc lang="text"] { "name": "Saaraan", "is_published": true, "website": "https://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 } } [/cc]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:[cc lang="php"] followers_count .'
'; echo 'Facebook Fans : '. $facebook_data->likes.'
'; echo 'Google Page Circle : '. $google_data->plusOneCount;echo '';
//print_r($twitter_data);
//print_r($facebook_data);
//print_r($google_data);
echo '
';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); } } ?> [/cc]Returns
Twitter Followers : 1525 Facebook Fans : 1100 Google Page Circle : 19