简体   繁体   中英

Getting Facebook friends gender

Following code gives me the id of the friends. What I am trying to do is get the gender of the friend but not having any success. I can run another loop with the generated ID but it won't be so efficient. Is there a query that will gives the name and the gender of the friends. The permission for the app is friends_about_me. Is this permission good enough to get the friends gender?

    <?php
      $app_id = '******';
      $app_secret = '*****';
      $my_url = 'http://apps.facebook.com/****/';

      $code = $_REQUEST["code"];

     //auth user
     if(empty($code)) {
        $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
        . $app_id . '&redirect_uri=' . urlencode($my_url) ;
        echo("<script>top.location.href='" . $dialog_url . "'</script>");
      }

      //get user access_token
      $token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
        . $app_id . '&redirect_uri=' . urlencode($my_url) 
        . '&client_secret=' . $app_secret 
        . '&code=' . $code;
      $access_token = file_get_contents($token_url);

      // Run fql query
      $fql_query_url = 'https://graph.facebook.com/'
        . '/fql?q=SELECT+uid2+FROM+friend+WHERE+uid1=me()'
        . '&' . $access_token;
      $fql_query_result = file_get_contents($fql_query_url);
      $fql_query_obj = json_decode($fql_query_result, true);


    echo $fql_query_result;

You just need a single FQL query to get gender information for your user's friends:

SELECT uid, name, sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

This was adapted from a sample on Facebook Developers FQL main page .

Note that not all users will have a published gender, so you should take this into account when writing your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM