skip to Main Content

I try to get the total number of likes for different Facebook pages, like this one for example : https://www.facebook.com/LeoMessi/

I have implemented the following code :

<?php
// Pass session data over.
session_start();

// Include the required dependencies.
require_once( 'vendor/autoload.php' );

// Initialize the Facebook PHP SDK v5.
$fb = new FacebookFacebook([
    'app_id'                => '**********',
    'app_secret'            => '**********',
    'default_graph_version' => 'v2.9',
]);

try {
    // Returns a `FacebookFacebookResponse` object
    $response = $fb->get('/176063032413299?fields=likes.limit(0).summary(true)', $_SESSION['access_token']);
    var_dump($response);
} catch(FacebookExceptionsFacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

It works, but it does not return the total_count property. I don’to how to access this property for any Facebook Page. Could you help me ?

2

Answers


  1. Chosen as BEST ANSWER

    Ok everybody, I just founded the solution just after having posted my question. And the solution is just here : Request to get total count of Facebook page likes in v2.3 API

    To get the number of fans, you now need to use fields=fan_count.

    So I just replace

    /176063032413299?fields=likes.limit(0).summary(true)
    

    With

    /176063032413299?fields=fan_count
    

    And it returns

    ["fan_count"]=> int(88899488)
    

    Hope this can help someone else.


  2. Modify field as :

    {page-id}?fields=fan_count
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search