简体   繁体   中英

Stuck on Graph API (facebook API), how do i get name, email and sex using this code?

Well, this is the issue. I have this code, who works very well:

The code

<?php

define('FACEBOOK_APP_ID', 'MY.APP.CODE');
define('FACEBOOK_SECRET', 'MY.APP.SECRET');

function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}

$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($cookie) {
      echo("The ID is: " . $cookie['uid']);
      echo('<br>The profile Picture is: <IMG SRC="http://graph.facebook.com/' . $cookie['uid'] . '/picture?type=large"><br>');
    } else {
      echo('<fb:login-button perms="email,user_birthday,publish_stream,user_status"></fb:login-button>');
    } ?>

    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
               cookie: true, xfbml: true});
      FB.Event.subscribe('auth.login', function(response) {
        window.location.reload();
      });
    </script>
  </body>
</html>

The output

The id is: A number here The picture is: A picture here

The problem

As you can see, this code give me the facebook's ID of the user and the profile picture when he login and give the permissions to the app. As i said, this code works fine, but i need more information, how do i retrive the email, the sex and the name using the same logic of this code?. Please do not use external libiries (like the facebook.php )

Remember that this is a very simple app so i dont want to get it complex in the code, i know that the solution have something to do with parsing JSON responses, but i cant find any good and simple code. Thanks!!!!

Graph API returns result as JSON.

For example

GET http://graph.facebook.com/4 HTTP/1.1

gives

{"id":"4","name":"Mark Zuckerberg","first_name":"Mark","last_name":"Zuckerberg","link":"http:\/\/www.facebook.com\/zuck","gender":"male","locale":"en_US"}

Here name is name; gender is sex. If you have granted email permission from user, you got the email also.

You need to look at all the extended permissions that you may need to ask for based on what data you need.

http://developers.facebook.com/docs/authentication/permissions

'me/friends?fields=gender,name,id'

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