Get Facebook share count of any URL - Graph API
up 5 down 1
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.

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

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.

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

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.

$apiUrl = '{engagement}&access_token=YOUR-ACCESS-TOKEN';
//open connection
$ch = curl_init();
//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
$data = json_decode($result,true);
echo '
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.

    var getcounturl = "";
    $.getJSON(''+getcounturl+'&fields=og_object{engagement}&access_token=YOUR-ACCESS-TOKEN', function (fbdata) {
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

   var permalinkWPSC = '';
   var token = 'App Token';
   function updateShareCount (permalinkWPSC,token) {
   return jQuery.ajax({
        url: ''+permalinkWPSC+'&access_token='+token+'&fields=og_object{engagement}',
        type: 'GET',
        dataType: 'JSONP',
        success: function(data) {
        	$( ".getcount" ).html( data.og_object.engagement.count);


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 *