facebook graph api check if user is a member of a group using PHP

守給你的承諾、 提交于 2019-11-29 15:18:42

问题


i want to check if a user is a member of a group using facebook graph api...

i have this:

$group = file_get_contents("https://graph.facebook.com/177129325652421/members?access_token=XXXXXXXX");
$group = json_decode($group);

$checkuser = $group->data;

and check the user if is a member by using his facebook id and in_array()

if(in_array($_GET["fid"],$checkuser)){

echo "yes";

} else {

echo "no";
}

can someone help me to correct this please... my code is not working...


回答1:


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

Use the API url:

https://graph.facebook.com/me/groups

To get a user's groups. In the above link, change the me/ to the user's FB ID. You must also pass in an Access Token.

The reply will be JSON encoded. Decode it using json_decode to a PHP Associative array. Iterate over it and check for the group you want.

The Graph API does not return all groups at once. You must either use the pagination links at the end of each response to fetch more, or use the limit parameter to request as many as you need.

The following code sample will post the IDs of the Groups you are a part of

<?php

$url = "https://graph.facebook.com/me/groups?access_token=AAAAAAITEghMBAMDc6iLFRSlVZCoWR0W3xVpEl1v7ZAxJRI3nh6X2GH0ZBDlrNMxupHXWfW5Tdy0jsrITfwnyfMhv2pNgXsVKkhHRoZC6dAZDZD";
$response = file_get_contents($url);

$obj = json_decode($response);

foreach($obj->data as $value) {
    echo $value->id;
    echo '<br>';
}

/* to check for existence of a particular group 

foreach($obj->data as $value) {
    if ($value->id == $yourID) {
        //found
        break;
    }

    //not found. fetch next page of groups
}

*/

PS - If running the above code gives you an error stating Could not find wrapper for "https", you need to uncomment/add the PHP extension extension=php_openssl.dll




回答2:


Was looking into this and found this as first answer in google but the answer seems to be much of a hassle so I dug a bit deeper.

The fastest answer I've found which doesn't require iterating through all of the groups' members uses FQL.

SELECT gid, uid FROM group_member WHERE uid = (user id) AND gid = (group id)

This either returns an empty 'data' object, or a 'data' object with the UID and GID.

It also (from what I see so far) , doesn't require the user_groups permission.

https://developers.facebook.com/tools/explorer?fql=SELECT%20gid%2C%20uid%20FROM%20group_member%20WHERE%20uid%20%3D%20551549780%20AND%20gid%20%3D%20282374058542158

This FQL query returns for me:

{
  "data": [
    {
      "gid": "282374058542158", 
      "uid": "551549780"
    }
  ]
}



回答3:


This doesn't seem to be possible after Graph API v2.4, because Facebook decided to disallow it: https://developers.facebook.com/docs/apps/changelog#v2_4

"the user_groups permission has been deprecated. Developers may continue to use the user_managed_groups permission to access the groups a person is the administrator of. This information is still accessed via the /v2.4/{user_id}/groups edge which is still available in v2.4."

It also states "From October 6, 2015 onwards, in all previous API versions, these endpoints will return empty arrays." But it seems to me that it still works on v2.2 & v2.3.



来源:https://stackoverflow.com/questions/10393045/facebook-graph-api-check-if-user-is-a-member-of-a-group-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!