Get Facebook share count of any URL - Graph API
0
up 4 down 0
Thank you for voting !

Get Facebook share count of any URL – Graph API

In this tutorial I will explain you about how to fetch the URL share count of Facebook using Graph API. Before we proceed let us talk about URL share counts.

What are URL share counts ?

On Facebook platform when any website URL is shared by someone its counted numerically and stored on Facebook server database and its knows the URL share counts. Getting Facebook share count of any URL is sometime important for developers.
Days are gone when share count was fetched without access token. Facebook updated its API endpoints and we need to generate an access token before we get JSON response on browser.

In this article I will cover everything from creating a Facebook App to get count value on browser. So without wasting the users read time, let’s proceed !

How to Get an access token ? (Follow Step by Step)

Step 1 – sign up/login to Facebook developer account.

Step 2 – Once logged in to Facebook developer platform. Click on My apps -> Create Apps on top right.
facebook-app-create

Step 3 – Now a pop-up will appear, fill up the display name field and click create App ID.
facebook-app-create

Step 4 – Once the App is built, its better to enter the Domain name in “App domains” field and save changes. The App will provide an App ID & App secret. You can generate your access token using the App ID & App secret via third party services but its pretty secure to get Access Token from official Facebook tool. The below screenshot demonstrate how the App ID & App secret looks.
facebook-app-create

Step 5 – Now get your Access Token(App Token) using the official Facebook link Click here. Make sure you are logged in before you click the link. The Access token looks like this
facebook-app-create


Finally we have the Access Token and we need to send request to Facebook server to get JSON response back to our browser. There are various methods like CURL, JS, JQUERY AJAX to send request and get the count value. Let’s proceed with CURL.

Method 1 – Execute CURL code on .php file extension.

<?PHP
$apiUrl = 'https://graph.facebook.com/v4.0/?id=https://google.com&fields=og_object{engagement}&access_token=YOUR-ACCESS-TOKEN';
//open connection
$ch = curl_init();
$timeout=5;
//set the url
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$data = json_decode($result,true);
echo '
';
print_r($data);
echo '
'; echo '

Likes: '.number_format($data['og_object']['engagement']['count']).'

'; ?>


Method 2 – Execute Javascript JS code on .js file extension. Don’t forget to include JQuery Library in head section.

<script>
    var getcounturl = "https://google.com";
    $.getJSON('https://graph.facebook.com/v4.0/?id='+getcounturl+'&fields=og_object{engagement}&access_token=YOUR-ACCESS-TOKEN', function (fbdata) {
    $('.getcount').html('
Total Shares ' + fbdata.og_object.engagement.count) }); </script>


Method 3 – Get the share count using JQuery AJAX. Include the Jquery library before the below code is executed on front-end

<script>
   var permalinkWPSC = 'https://google.com/';
   var token = 'App Token';
   
   updateShareCount(permalinkWPSC,token);
   
   function updateShareCount (permalinkWPSC,token) {
   return jQuery.ajax({
        url: 'https://graph.facebook.com/v4.0/?id='+permalinkWPSC+'&access_token='+token+'&fields=og_object{engagement}',
        type: 'GET',
        dataType: 'JSONP',
        success: function(data) {
	//console.log(data);
        	$( ".getcount" ).html( data.og_object.engagement.count);
        }
    });
 }
</script>


Conclusion

Getting the Facebook share count using graph API is pretty easy and straight forward task. You will find many articles on how to get Facebook count values using graph API but trust me very few of them are working examples because Facebook updated its API end-point and recently removed “JSON share field”.
Finally we have the value to display anywhere on our blog or business websites. Instead of using Plugin to fetch the value, use above code examples to save website performance and decrease load time. Comment, if you still have any issue, I will be glad to assist you.

Something To Say ?

Your email address will not be published. Required fields are marked *

*