FB API to Pull the Like and Share Statistics for a URL

By fathimath.sajila | 27th November 2017 | 1 min read

Here we describe the steps to find number of likes and shares for an article listed on your website. We can pull the count of like and share for a URL using API links:

https://api.facebook.com/method/links.getStats?urls=YOUR_URL_HERE&format=json

The json response is like this

[{"url":"www.mywebsite.com","normalized_url":"http:\/\/www.mywebsite.com\/","share_count":88,"like_count":26,"comment_count":6,"total_count":120,"click_count":0,"comments_fbid":"10150230611357724","commentsbox_count":0}]

Checkout the article to find the steps to create a Facebook App. We can also pull the like and share statistics using PHP Facebook SDK. For this purpose we need the following.

  1. Download PHP Facebook SDK.
  2. Include autoload.php in all sample of thecode.

Let's look at a simple example:

require_once __DIR__ . '/your_path/src/Facebook/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
$session = new FacebookSession('YOUR_ACCESS_TOKEN');
try {
  $request = new FacebookRequest(
  $session,
  'GET',
  '/',
  array (
    'id' => 'http://www.mywebsite.com',
  )
);
  $response = $request->execute();
  $graphObject = $response->getGraphObject();
  print_r($graphObject);
}
catch (\Exception $e) {
  print_r($e);
}

Hope this helps.