Jimmy M
Jimmy M

Reputation: 1627

Getting Facebook Like count

I have integrated Facebook Like into my website.Now i need to get/list the count of the Facebook like in the Admin panel of my website. Is there any way to get this done?Need suggestions.Thanks in advance.

Upvotes: 3

Views: 22236

Answers (4)

Samuel Guebo
Samuel Guebo

Reputation: 36

You can achieve that using the Graph API (v3)

function checkFacebookReactions($url){
    $access_token = 'YOUR-FACEBOOK-TOKEN'; // Generate a Facebook Token first

    $api_url = 'https://graph.facebook.com/v3.0/?id=' . urlencode( $url ) . '&fields=engagement&access_token=' . $access_token;
    $fb_connect = curl_init(); // initializing
    curl_setopt( $fb_connect, CURLOPT_URL, $api_url );
    curl_setopt( $fb_connect, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
    curl_setopt( $fb_connect, CURLOPT_TIMEOUT, 20 );

    $json_return = curl_exec( $fb_connect ); // connect and get json data
    curl_close( $fb_connect ); // close connection
    $body = json_decode( $json_return );

    // Print each key values, if needed
    foreach($body->engagement as $k=>$v){
        echo $k .": " .$v ."<br>";
    }

    $count = $body->engagement->reaction_count; // Return only reactions (likes + other reactions)
    return $count; 
}

$url = "REPLACE-WITH-YOUR-URL"; // The url you want to fetch details from
checkFacebookReactions($url);

First, you will need to generate a Facebook token. You can fin plenty of resources that explain how to get a token. This one will do the trick: https://elfsight.com/blog/2017/10/how-to-get-facebook-access-token

Upvotes: 0

krishna singh
krishna singh

Reputation: 1073

function fbLikeCount($appid,$appsecret){
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$appid.'?access_token='.$appsecret;
$json = file_get_contents($json_url);
$json_output = json_decode($json);

//Extract the likes count from the JSON object
if($json_output->likes){
    return $likes = $json_output->likes;
}else{
    return 0;
}

}

Facebook - <?php echo fbLikeCount('app/page id here','token here'); ?>

Upvotes: 0

Ajeet  Sinha
Ajeet Sinha

Reputation: 2345

In order to get the number of likes of any object using Facebook API, use a Facebook API of your choice and commands like this:

 https://graph.facebook.com/< your object id>/

You will receive a JSON object and you can extract the number of likes from it:

 {
 "id": "567454",
 "link": "http://www.facebook.com/pages/PHP-Developer/103146756409401",
 "likes": 250,
 "type": "page"
 }

More info on https://developers.facebook.com/docs/reference/api/

The more direct / updated link for this topic on facebook is

https://developers.facebook.com/docs/reference/api/page/

Upvotes: 8

enzyme42
enzyme42

Reputation: 101

$url = 'http://graph.facebook.com/PAGE_OR_APP_ID';
echo '['.$url.']: '.json_decode(file_get_contents($url))->{'likes'};

Upvotes: 10

Related Questions