Skip to content

Instantly share code, notes, and snippets.

@samuelguebo
Last active September 15, 2018 12:24
Show Gist options
  • Save samuelguebo/7253340809b5de05ca96a7594f62786d to your computer and use it in GitHub Desktop.
Save samuelguebo/7253340809b5de05ca96a7594f62786d to your computer and use it in GitHub Desktop.
Quickly find the Facebook Like count for any url using Graph api (v3)
<?php
function checkFacebookReactions($url){
// Generate a token https://elfsight.com/blog/2017/10/how-to-get-facebook-access-token/
$access_token = "YOUR-FACEBOOK-TOKEN";
$api_url = 'https://graph.facebook.com/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&access_token=' . $access_token;
$fb_connect = curl_init(); // initializing
curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $fb_connect ); // connect and get json data
curl_close( $fb_connect ); // close connection
$body = json_decode( $json_return );
// Print all engagement count if needed
foreach($body->engagement as $k=>$v){
echo $k .": " .$v ."<br>";
}
// Return only reactions (likes + other reactions)
$count = $body->engagement->reaction_count;
return $count;
}
$url = "REPLACE-WITH-YOUR-URL"; // The url you want to fetch details from
checkFacebookReactions($url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment