Get Facebook share count – Graph API | Infoconic

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

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


Get Facebook share count of any URL – Graph API

Read latest Technology News & never miss out

What are URL share counts ?

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

Conclusion

Something To Say ?

Categories
Recent Posts

<?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 '';
echo '';
?>
';
print_r($data);
echo '

<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>

<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>

My apps -> Create AppsCURLJSJQUERY AJAX.php.jsDays are gone when share count was fetched without access token.we need to generate an access token before we get JSON response on browser“JSON share field”PreviousNextClick herecodesHow toOct 20190up
5
down
1
, **

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.

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.
Facebook updated its API endpoints and .
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 !

Step 1 – sign up/login to Facebook developer account.
Step 2 – Once logged in to Facebook developer platform. Click on 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 . 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 , , to send request and get the count value. Let’s proceed with CURL.
Method 1 – Execute CURL code on file extension.

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

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

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 .
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.

Scroll to Top