Facebook API: How to get count of group members

2019-04-12 19:11发布

问题:

Im working on fb app using PHP client and FQL.Looks like that fb api doesnt support this. You can get max random 500 members with FQL. But when group has more than 500 members there is no way how to get total count of members. I need only number dont care about member details.

Anyone can help me please?

回答1:

You can use the summary parameter which will return a total_count value.

/v2.11/{GROUP_ID}?fields=members.limit(0).summary(true)

It also works when searching for groups:

/v2.11/search?q=stackoverflow&type=group&fields=id,name,members.limit(0).summary(true)

{
    "data": [
        {
            "id": "667082816766625",
            "name": "Web Development Insiders",
            "members": {
                "data": [
                ],
                "summary": {
                    "total_count": 2087
                }
            }
        },
        {
            "id": "319262381492750",
            "name": "WEB Developers India",
            "members": {
                "data": [
                ],
                "summary": {
                    "total_count": 10240
                }
            }
        }...
    ]
}


回答2:

I have actually found that you cannot, it is by design apparently. I wanted to know this myself about a month ago, and found that no matter what parameters you pass in to the graph api, you can not get past the 500th member. Even if you tell it to start at number 450 and give you 200, it will give you only 450-500.

Here's me asking, and the unfortunate answer:

http://forum.developers.facebook.net/viewtopic.php?id=82134



回答3:

you can use graph api 2.0 and sdk 4.0

you can use below code

$url1= "https://graph.facebook.com/".$gid."/members?limit=10000&access_token=".$_SESSION['fb_token'];





if(!extension_loaded('curl') && !@dl('curl_php5.so'))
    {
        return "";
    }



    $parsedUrl = parse_url($url1);
    $ch = curl_init();
    $options = array(
        CURLOPT_URL => $url1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_HTTPHEADER => array("Host: " . $parsedUrl['host']),
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => false
    );
    curl_setopt_array($ch, $options);
    $response_count = @curl_exec($ch);
    $json_count = json_decode($response_count); 

        foreach ($json_count as $item_count)
        { 


                    $cnt=count($item_count);
                    break;

        }


回答4:

Make a query to:

https://graph.facebook.com/v2.11/{group-id}/members?limit=1500&access_token={token}

(replace {group-id} by the numeric group identification and {token} by your access token).

You will get a response like this:

{
    "data": [
        {
            "name": "Foo bar",
            "id": "123456",
            "administrator": false
        },
        ...
    ],
    "paging": {
        "cursors": {
            "before": "QVFIUkwzT3FKY0JSS2hENU1NUlZAocm9GelJMcUE3S1cxWWZAPYWx1cUQxcXFHQUNGLUJnWnZAHa0tIdmpMT0U5ZAjRBLUY5Q2ZAGbmwwVTNoSHhLc1BCc2dvVTF3",
            "after": "QVFIUkFoU3lYR2tXc09adkg5OGhlbHRWRk1GYkZAzQU1DalRSY05zOVl5aE1tcjRMS3lXLURaVWNMOGZArWTVxS2hPQUVGVWxhbXZAyZA0p3azVKM2hBSEp3YlpR"
        }
    },
    "next": "https://graph.facebook.com/v2.11/123928391023981/members?access_token=EAACEdEose0cBALBDrdgLyVOjzW4mz6G3d3Yj1fTGYqygVgYq0JCDZAi0zYsY90pSSQ9hQZCn3TdwfXIAiyoXH5oUYcA4hOcCI9jztkkUhbBv9tEQ3ZBEEuHpmkm3kmgvk1HNq5mo6BM0hz8XkOLVh3twIdz83KhB9SkqxuxHeFD9GWsQqjys6XTuL2315QZD&pretty=0&limit= 1500&after=QVFIUkFoU3lYR2tXc08adkg5OGhlbHRWYk1GYkZAzQU1DalRSY05zOVl5aQ1tcjRMS3lXLURaVWNMOGZArWTVxS2hPQUVGVWxhbXZAyZA0p3azVKM1hBSEp3YlpR"
}

Then follow this algorithm:

  1. Make a count variable with a 0 value.
  2. Count the objects in the data array of the latest response and add the count to the count variable.
  3. If the latest response has the next property, make a request to the URL which is the next property value and return to the step 2. Otherwise you have finished, the count of members is the count variable value.

This way is not very good because the more members there are in the group the more queries are required. I would be better to parse the number of members from the group page HTML but I can't find reliable way to do it.

Update 2017.10.19: If the Facebook API response size is more then about 345KB, Facebook returns an error: Please reduce the amount of data you're asking for, then retry your request. It is about 1997 members. So you need to set the limit request parameter to 1500 not to face the error.

Update 2018.01.26: There is a way to get a count of members using a single request: https://stackoverflow.com/a/47783306/1118709

Update 2018.01.31: After 90 days from releasing Graph API v2.12 the request will require an access token of an admin of the group. Source: Graph API docs / v2.12 changelog / 90-day breaking changes



回答5:

You should do it with an FQL query on the group_member table like:

SELECT uid FROM group_member WHERE gid = <group_id> limit 500 offset 500

Example here: Is it possible / how to get number of a particular Facebook Group members (even if number of them is 500+)?