Skip to content

Instantly share code, notes, and snippets.

@hlorand
Created December 29, 2017 22:32
Show Gist options
  • Select an option

  • Save hlorand/2613fc48389ddda72e7f78c3617f6c6f to your computer and use it in GitHub Desktop.

Select an option

Save hlorand/2613fc48389ddda72e7f78c3617f6c6f to your computer and use it in GitHub Desktop.
PHP Facebook reactions(like,wow,sad,angry,love,haha,thankful) counter
<!DOCTYPE html>
<html>
<head>
<title>Facebook reactions counter</title>
<meta http-equiv="refresh" content="60">
<meta charset="UTF-8">
<base target="_blank">
</head>
<body>
<?php
//THIS CODE COUNTS FACEBOOK REACTIONS(like,wow,sad,angry,love,haha,thankful) ON FACEBOOK POSTS/IMAGES
/*****************************************************************
ACCESS TOKEN
------------
0. To get a Graph API access token you need a Facebook application. If you don't have one, create:
https://developers.facebook.com/apps/
1. Get a temporary access token here: https://developers.facebook.com/tools/explorer/
Select your Facebook application from a Dropdown.
Press "Get token button > Get user access token > select permissions > Submit"
2. This access token lives only 60 minutes. If it's okay, go to step 3. To get a longer access token visit this URL:
https://graph.facebook.com//oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={temporary_token}
Fill out the braces with the required info. app_id and app_secret can be obtained from the first link, temporary_token from the second link.
3. Paste your temporary or longer living token here:
******************************************************************/
$atoken = 'EAACEdEose0cBAIWpiu7nSf9fDEexcVzIJdsVSz7RS1PrFPpONCuLkAHwgA7Ux4aDfGsZB00reSk0KXILfAAqGSaH6z8XGM1GpYZBVhpSTPXja6IiWLXA5R8iFrlhVJEIi7st9zbv2RgqKU2uyVdkgZAMZCdwYlgl6wUEr8IcQgshMcXFfUzQ4LzYtB3NBr0ZD';
/*****************************************************************
Facebook post IDs
------------
This is a comma separated list of post or image IDs. You can get these from the post URL.
The script counts the reactions on these.
******************************************************************/
$ids = '1560724960654749,1560725017321410';
/*****************************************************************
Feel free to modify the script below. Currently every reactions summed up in the $sum variable.
If you need to separately count certain reactions modify the script.
******************************************************************/
$data = file_get_contents('https://graph.facebook.com/v2.6/?ids=' . $ids . '&fields=reactions.type(LIKE).limit(0).summary(total_count).as(reactions_like),reactions.type(LOVE).limit(0).summary(total_count).as(reactions_love),reactions.type(WOW).limit(0).summary(total_count).as(reactions_wow),reactions.type(HAHA).limit(0).summary(total_count).as(reactions_haha),reactions.type(SAD).limit(0).summary(total_count).as(reactions_sad),reactions.type(ANGRY).limit(0).summary(total_count).as(reactions_angry),reactions.type(THANKFUL).limit(0).summary(total_count).as(reactions_thankful)&access_token=' . $atoken);
$data = json_decode($data,true);
$entries = array();
//sum up the reactions
foreach ($data as $key => $value) {
$sum = $value['reactions_like']['summary']['total_count'];
$sum += $value['reactions_love']['summary']['total_count'];
$sum += $value['reactions_wow']['summary']['total_count'];
$sum += $value['reactions_haha']['summary']['total_count'];
$sum += $value['reactions_sad']['summary']['total_count'];
$sum += $value['reactions_angry']['summary']['total_count'];
$sum += $value['reactions_thankful']['summary']['total_count'];
$entries[$key] = $sum;
}
arsort($entries); //sorting by reactions count
//echoing the results
foreach ($entries as $key => $value) {
echo 'ID: <a href="http://fb.com/' . $key . '">' . $key . '</a> Reactions: ' . $value . '<br>';
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment