Request to get total count of Facebook page likes in v2.3 API

前端 未结 3 2146
攒了一身酷
攒了一身酷 2020-12-31 10:13

Previously I was using FQL for this, but this is deprecated as of v2.1 and I\'m moving over to v2.3 using the graph edge \"likes\".

Here is my URL:

h         


        
相关标签:
3条回答
  • 2020-12-31 10:47

    Anyone stumbling onto this answer now (April 2016) will get frustrated because the accepted answer no longer works in v2.6

    ?fields=likes and /likes now return the same result -> the pages that the page likes.

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

    https://graph.facebook.com/pepsius/?fields=fan_count&access_token=<access_token>
    

    As you can see above, you can also make the request directly with the pagename, no need to fetch the pageID.

    0 讨论(0)
  • 2020-12-31 10:55

    Thanks @NativePaul

    I spent almost two days to find a solution to get the Facebook Fan Page likes counter in numeric value to a shortcode. So I have amended a code I got from this link: http://www.internoetics.com/2015/07/13/display-number-facebook-page-likes-wordpress-php/

    And amended it to work with the fan_count fields and here is the code for your reference:

    /*
    	Display the Number of Facebook Page Likes in Plain Text with WordPress Shortcode (and PHP)
    	Shortcode: [fbpagelikes id="" appid="" appsecret="" cache="" n="1"]
    */
    
    
    function internoetics_fb_pagelikes($atts) {
      extract(shortcode_atts(array(
        'id' => 'kenryscom',
        'appid' => 'xxxxxxxxxxxxxxxx',
        'appsecret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'n' => 1,
        'cache' => 3600 * 24 * 1
      ), $atts));
    
     $fbcounthash = md5("$url.$cache.$appid.$appsecret.$n");
     $fbcountrecord = 'fblikes_' . $fbcounthash;
     $cachedposts = get_transient($fbcountrecord);
     if ($cachedposts !== false) {
     return $cachedposts;
    
      } else {
    
      $json_url ='https://graph.facebook.com/' . $id . '?fields=fan_count&access_token=' . $appid . '|' . $appsecret;
      $json = file_get_contents($json_url);
      $json_output = json_decode($json);
     
      if($json_output->fan_count) {
       $fan_count = $json_output->fan_count;
       if ($n) $fan_count = number_format($fan_count);
       set_transient($fbcountrecord, $fan_count, $cache);
       return $fan_count;
        } else {
       return 'Unavailable';
      }
     }
    }
    add_shortcode('fbpagelikes','internoetics_fb_pagelikes');

    You have to add the above code to the theme functions file and use the Shortcode anywhere as mentioned in the beginning of the code.

    0 讨论(0)
  • 2020-12-31 11:06

    What you are looking for the total number of people that have liked the page or what the page has liked?

    For example.

    https://graph.facebook.com/v2.3/56381779049/likes?access_token=<access_token>&summary=true
    

    Will return what the Page PepsiUS has liked.

    https://graph.facebook.com/v2.3/56381779049?fields=likes&access_token=<access_token>
    

    Will return the total number of people that have liked the page.

    {"likes": 32804486, 
    "id": "56381779049"}
    

    Varified here PepsiUS

    0 讨论(0)
提交回复
热议问题