4

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...

ekad
  • 14,436
  • 26
  • 44
  • 46
Zhia Aruza
  • 51
  • 1
  • 3

3 Answers3

5

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

Ayush
  • 41,754
  • 51
  • 164
  • 239
  • Please be more specific. What is not working? What error are you getting? – Ayush May 01 '12 at 03:22
  • i check it using graph api explorer.. and still no results...http://developers.facebook.com/tools/explorer?method=GET&path=100003473985021%2Fgroups – Zhia Aruza May 01 '12 at 03:22
  • The Graph API link works on the currently logged in user. That's what the `/me/` denotes. It will show you your groups, provided you are currently logged into FB. What message do you get when you navigate to that URL? – Ayush May 01 '12 at 03:24
  • Oh, just saw the link above. An empty data array could mean one of two things - Your access token isn't valid for the user you are querying (in which case I would suspect an error msg from FB), or the user has not joined any groups. – Ayush May 01 '12 at 03:25
  • i have joined a ton of groups... and im using a generated token from gaph api explorer with the id "/me" – Zhia Aruza May 01 '12 at 03:27
  • In the link you shared, change the url to `https://graph.facebook.com/me/groups`. Then click on the `Get Access Token` button, and make sure you select `user_groups`. – Ayush May 01 '12 at 03:27
  • this one works...https://graph.facebook.com/177129325652421/members?access_token=XXXXXXXX but it takes time searching for the id.... but atleast it works.... but the problem i'm facing is i dont know how to search it..using the users facebook id – Zhia Aruza May 01 '12 at 03:28
  • Once you get the repsonse, the code to search is in my answer. You need to decode the JSON response and iterate over it – Ayush May 01 '12 at 03:30
  • This is a way to get this to work. Unfortunately, it seems as if there is no /groupID/groups/memberID query to allow for checking if a user is a member of a group. – Diode Dan May 17 '15 at 14:51
3

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"
    }
  ]
}
wingerse
  • 3,670
  • 1
  • 29
  • 61
Shai Mishali
  • 9,224
  • 4
  • 56
  • 83
2

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.

Pascal Corpet
  • 266
  • 1
  • 7