Facebook图形得到喜欢/评论计数与PHP的帖子

I have tried many methods of doing this and all of them failed...

I'm using the facebook php sdk to create my own news feed(the feed comes from a page's timeline)

To get the likes in the facebook I use $post['likes'] where likes would output:

"data": [
           {
              "id": "100001919135377",
              "name": "Person name"
           },
           {
              "id": "1153253855",
              "name": "Person name"
           },
           {
              "id": "100000945245573",
              "name": "Person name"
           },
           {
              "id": "100002595937528",
              "name": "Person name"
           },
           {
              "id": "100001873157306",
              "name": "Person name"
           },
           {
              "id": "1356273210",
              "name": "Person name"
           }
        ]

How can I count the number of likes and then echo that?

Same thing for comments, I use $post['comments'] and it would output

"data": [
           {
              "id": "637555672952364_6191387",
              "from": {
                 "name": "Person Name",
                 "id": "1153253855"
              },
              "message": "Comment content",
              "can_remove": false,
              "created_time": "2014-01-11T07:28:37+0000",
              "like_count": 0,
              "user_likes": false
           },
           {
              "id": "637555672952364_6191388",
              "from": {
                 "name": "Person Name",
                 "id": "1153253855"
              },
              "message": "Comment content",
              "can_remove": false,
              "created_time": "2014-01-11T07:28:39+0000",
              "like_count": 0,
              "user_likes": false
           }
        ]

Same thing for this I would like to count the number of comments and echo it...

If you need to know more info please tellme.

Thanks in advance

use the count(); function to get the number of element that a JSON array has

Here's a simple PHP example with file_get_contents

<?php

function fetchUrl($url){

         return file_get_contents($url);

}

$authToken = "{Token}";

$json_object = fetchUrl("https://graph.facebook.com/{POST_ID}/likes?$authToken}&limit=5000"); // 

$feedarray   = json_decode($json_object, true);

$likesNum = count($feedarray['data']); // return the number of items in `data` array

print $likesNum;

?>

same thing works for comments as well

Try using the comments.summary(true) in the api call

eg Adding count to the comments

Here is a easy way to get facebbok like count from Facebook

<?php
    function fetchUrl($url) {
        return file_get_contents($url);
    }

    $authToken = "{token}"; // Authentication Token
    $facebookUrl = "{page name only}"; // https://www.facebook.com/page name
    $json_object = fetchUrl("https://graph.facebook.com/$facebookUrl/?fields=fan_count&access_token=$authToken");

    $feedarray   = json_decode($json_object, true);

    echo number_format($feedarray['fan_count'],0,",","' "); // Will display count : 00'000
    echo $feedarray['fan_count']; // Will display count : 00000
 ?>

You can use this

$json_object = fetchUrl("https://graph.facebook.com/{POST_ID}/likes?field=total_count");