Skip to content

Instantly share code, notes, and snippets.

@andrewspear
Last active November 2, 2018 19:50
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save andrewspear/6348709 to your computer and use it in GitHub Desktop.
Save andrewspear/6348709 to your computer and use it in GitHub Desktop.
Count your Facebook followers, Twitter followers, Google+ followers, LinkedIn group followers, Constant Contact subscribers, YouTube subscribers, YouTube channel views.
<?
/*
$facebook_followers = social_count('facebook', array(
'id' => '<your_facebook_page_name_or_id>',
'api_key' => '<your_facebook_app_api_key>',
'api_secret' => '<your_facebook_app_api_secret>'
));
$twitter_followers = social_count('twitter', array(
'id' => '<your_twitter_username>',
'oauth_access_token' => '<your_twitter_oauth_access_token>',
'oauth_access_token_secret' => '<your_twitter_oauth_access_token_secret>',
'consumer_key' => '<your_twitter_consumer_key>',
'consumer_secret' => '<your_twitter_consumer_secret>'
));
$gplus_followers = social_count('gplus', array(
'id' => '<your_google_plus_id>',
'apiKey' => '<your_google_api_key>'
);
$linkedin_group_followers = social_count('linkedin_group', array(
'id' => '<your_linkedin_group_username>',
'appKey' => '<your_linkedin_app_key>',
'appSecret' => '<your_linkedin_app_secret>'
));
$constant_contact_subsribers = social_count('constant_contact', array(
'username' => '<your_constant_contact_username>',
'password' => '<your_constant_contact_password>',
'apiKey' => '<your_constant_contact_api_key>',
'lists' => array(<id_of_constant_contact_lists_to_be_counted>)
));
$youtube_views = social_count(array(
'channel' => '<your_youtube_channel_name>',
'data' => 'totalUploadViews',
'api_key' => '<your_youtube_api_key>'
));
$youtube_subscribers = social_count(array(
'channel' => '<your_youtube_channel_name>',
'data' => 'subscriberCount',
'api_key' => '<your_youtube_api_key>'
));
*/
function social_count($network, $settings = null) {
// Make user $settings is an associative array
if (!empty($settings) && !is_array($settings)) {
$s = $settings;
$settings = array();
$settings['id'] = $s;
}
// Fetch counts from networks
switch ($network) {
// Required $settings values = username (string), password (string), apiKey (string), lists (array)
// To get all lists query https://api.constantcontact.com/ws/customers/{$settings['username']}/lists
case 'constant_contact':
$count = 0;
foreach ($settings['lists'] as $list) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.constantcontact.com/ws/customers/{$settings['username']}/lists/{$list}");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "{$settings['apiKey']}%{$settings['username']}:{$settings['password']}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/atom+xml"));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl = @curl_exec($ch);
curl_close($ch);
$data = new SimpleXMLElement($curl);
$count += $data->content->ContactList->ContactCount;
}
return $count;
break;
// Required $settings values = id (your group id, string),
// Dependency: http://oauth.googlecode.com/svn/code/php/OAuth.php
// Dependency: http://simple-linkedinphp.googlecode.com/svn/trunk/v3/3.3/3.3.0/linkedin_3.3.0.class.php
case 'linkedin_group':
require_once(dirname(__FILE__) . '/OAuth.php');
require_once(dirname(__FILE__) . '/linkedin_3.2.0.class.php');
$linkedin = new LinkedIn($settings);
$group = $linkedin->group($settings['id'], ':(num-members)');
$xml = new SimpleXMLElement($group['linkedin']);
return intval($xml->{'num-members'});
break;
// Required $settings values = id (your username, string), app_id (string), app_secret (string)
case 'facebook':
$data = json_decode(file_get_contents('https://graph.facebook.com/' . $settings['id'] . '?fields=likes&access_token=' . $settings['app_id'] . '|' . $settings['app_secret']));
return $data->likes;
break;
// Required $settings values = id (your username, string), oauth_access_token (string), oauth_access_token_secret (string), consumer_key (string), consumer_secret (string)
// Dependency: https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php
case 'twitter':
require_once(dirname(__FILE__) . '/TwitterAPIExchange.php');
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=' . $settings['id'];
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count = $twitter->setGetfield($getfield)->buildOauth($ta_url, $requestMethod)->performRequest();
$data = json_decode($follow_count, true);
return $data[0]['user']['followers_count'];
break;
// Required $settings values = id (your user id, string), apiKey (string)
// You can get a free Google+ API key here: https://console.developers.google.com
case 'gplus':
if ($data = file_get_contents('https://www.googleapis.com/plus/v1/people/' . $settings['id'] . '?fields=circledByCount%2CplusOneCount&key=' . $settings['apiKey'])) {
$data = json_decode($data);
return $data->circledByCount;
}
break;
// Required $settings values = channel (string), data (viewCount|commentCount|subscriberCount|hiddenSubscriberCount|videoCount), api_key (string)
case 'youtube':
if ($json = @file_get_contents("https://www.googleapis.com/youtube/v3/channels?key={$settings['api_key']}&part=contentDetails,statistics&forUsername={$settings['channel']}")) {
$youtube = json_decode($json);
$data = (in_array($settings['data'], array('viewCount', 'commentCount', 'subscriberCount', 'hiddenSubscriberCount', 'videoCount'))) ? $settings['data'] : 'viewCount';
if (isset($youtube->items[0]->statistics->$data)) {
return $youtube->items[0]->statistics->$data;
}
}
break;
}
}
?>
@jfbuildstuff
Copy link

Hey Andrew,

First off, thanks for this incredibly useful include. I looked for hours to find something like this where a bunch of use cases are already done for you :) very helpful!

Just a couple things I noticed when using the code here that will help future users.

In the comment area at the top the arrays for YouTube are missing the network arguments and the = signs should be => instead. Like so:


$youtube_likes = social_count('youtube', array(
    'channel' => 'your_youtube_channel_name'
));
 
$youtube_subscribers = social_count('youtube', array(
    'channel' => 'your_youtube_channel_name',
    'data' => 'subscriberCount'
));

Otherwise PHP doesn't read it as an array and sends a parse error.

Hope this makes it to your next revision round!

Thanks again,

James~

@ihorvorotnov
Copy link

Hey Andrew,
I'm working on a Google Plus code right now. Seems like you code returns nothing. I inspected G+ profile page and changed regex to /class="Myb j0c">([0-9,]+)</i (currently count number can be found in <span class="Myb j0c">1,391</span> tag). Still trying to make it work.

@andrewspear
Copy link
Author

@jfbuildstuff Thanks, updated in revision 11

@andrewspear
Copy link
Author

@ihorvorotnov Unfortunately G+ is the least reliable of the networks as we're relying on an HTML scrape rather than a proper API. Did you get it working in the end?

@andrewspear
Copy link
Author

@ihorvorotnov Google now has a G+ API, so today I updated the code to revision 13 which uses the official API rather than scraping the data which is much more reliable.

@andrewspear
Copy link
Author

Facebook now requires authentication for fetching likes, revision 14 supports this.

@andrewspear
Copy link
Author

Upgraded YouTube API to v3

@adarshvsukumaran
Copy link

Hi.. i am facing some issues with linkedIn followers count. i have used "OAuth.php", "linkedin_3.2.0.class.php" files downloaded separately. Not showing any error, but the count showing zero. And could you please suggest a sample linkedin page id.
Thanks in advance...

@adarshvsukumaran
Copy link

Hi.. i am facing some issues with linkedIn followers count. i have used "OAuth.php", "linkedin_3.2.0.class.php" files downloaded separately. Not showing any error, but the count showing zero. And could you please suggest a sample linkedin page id.

If i print the response, i am getting something like this,

SimpleXMLElement Object
(
[status] => 400
[timestamp] => 1469718573534
[request-id] => KNNMSTAQJ2
[error-code] => 0
[message] => Invalid value {abc1234} in key {id}
)
Please correct me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment