简体   繁体   中英

Determine Facebook likes of a page via php?

I think Facebook changed something with their API. A few days ago my code stopped working. I used the following code to read the number of likes of a FB page:

<?php
$link = "http://graph.facebook.com/pagename";
$json = file_get_contents($link);
$json = json_decode($json);
?>

<div class="fb_likes">
<p><?hp echo $json->{'likes'}; ?>

Does anybody know what has changed and how I can read the number of likes now?

Thanks

You may retrieve fb like count with function below:

function facebook_count($url){
  $fql  = "SELECT share_count, like_count, comment_count ";
  $fql .= " FROM link_stat WHERE url = '$url'";
  $fqlURL = "https://api.facebook.com/method/fql.query?format=json&query=" . urlencode($fql);
  $response = file_get_contents($fqlURL);
  return json_decode($response);
}
$fb = facebook_count('https://www.facebook.com/pages/Coca-Cola/1517438451865061');
echo $fb[0]->like_count;

Hard to say if we only know that it "stopped working", but you do need to use an Access Token for that call now. You also have to specify the fields you want to get. For example:

$link = "https://graph.facebook.com/pagename?fields=name,likes&access_token=xxx";

Information about the different Access Tokens:

More information in the changelog: https://developers.facebook.com/docs/apps/changelog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM