0

I am working on my first Facebook app with the help of Facebook’s online documentation for app development.

The issue I am facing is while authenticating I also need the gender and name of the user. The screen shot available in the above link says the ‘basic information’ I am requesting from the user contains both of them. But I don’t know how to retrieve them, they are in a JSON array I guess but I am not much familiar with JSON so may be thats why or I need extra permission for them?

<?php

  $signed_request = $_REQUEST["signed_request"];
  list($encoded_sig, $payload) = explode('.', $signed_request, 2);
  $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
  if (empty($data["user_id"])) {
    echo("<script> top.location.href='" . $auth_url . "'</script>");
  } else {
    echo("<b class=\"welcome\">Welcome " . $data["user_id"] . ",</b><br/>");
  }
?>
matt
  • 78,533
  • 8
  • 163
  • 197
Maven
  • 14,587
  • 42
  • 113
  • 174
  • 1
    Last time I checked you couldn't trust the birthday field... too many different combinations. It might have the year, might have not... I think the order of day/month can also change according to lang/country maybe so no easy task there... – jribeiro Feb 19 '12 at 15:13

2 Answers2

1

The source of your data is the signed request, which according to the documentation has a limited set of properties about the user (the list is in that link).

In order to get more data about the user, or his friends, you need to make api requests. Some of the info is granted to your application once the user authorized it, such as:

When a user allows you to access their basic information in an auth dialog, you have access to their user id, name, profile picture, gender, age range, locale, networks, user ID, list of friends, and any other information they have made public.

and:

To get access to any additional information about the user or their friends you need to ask for specific permissions from the user.

(read more here: http://developers.facebook.com/docs/reference/api/permissions/).

If you want just the gender and the and name make a request to /me like it states here: http://developers.facebook.com/docs/reference/api/user/ without the need to ask any other permissions.

EDIT

If you want some code snippets try this thread: Get user's name from Facebook Graph API

Community
  • 1
  • 1
Nitzan Tomer
  • 155,636
  • 47
  • 315
  • 299
  • as you said that when user authorized the app without any extra permission by default ull get user id, name, profile picture, gender, age range etc... in my case user has authorized my app and the app can retrieve his user_id but i am confused how to get the other info such as name and gender etc of the user? – Maven Feb 20 '12 at 10:56
  • You make graph api requests, for example to get the user name: *echo json_decode(file_get_contents('http://graph.facebook.com/1157251270'))->name;* There are more examples in the thread I linked to in my original post – Nitzan Tomer Feb 20 '12 at 11:08
0

If you want get birthday data, you should add permission on getLoginUrl method.

output of print_r($data) will be Array ( basic info + birthday ). You can calculate age of users using birthday field and date function.

more information about Facebook permissions.

Try this code:

<?php
$config= array('appId'=>APP_ID, 'secret'=>APP_SECRET);
$facebook= new Facebook($config);
$user_id= $facebook->getUser();
if( $user_id ){
   try {
      $data= $facebook->api('/me');
          print_r($data);
      }catch( FacebookApiException $e ){
        $user_id = null;
        $e->getType();
        $e->getMessage();
    }
}
if( $user_id ){
    $href= $facebook->getLogoutUrl();
    $text= "LogOut";
}else{
    $permission= array('scope'=>'user_birthday');
    $href= $facebook->getLoginUrl($permission);
    $text= "LogIn";
}
?>
<a href="<?php echo $href; ?>"><?php echo $text; ?></a>
Joko Wandiro
  • 1,957
  • 1
  • 18
  • 28
  • 1
    Dont think this is the right answer to the question, since https://developers.facebook.com/docs/authentication/permissions/ suggest that age range can be obtained with out extra permission. By default, this includes certain properties of the User object such as id, name, picture, gender, their locale and their age range. – pal4life Mar 26 '12 at 21:41