显示Facebook用户的性别和其他


Displaying a Facebook user's sex and others

我正在Facebook中启动一个应用程序,并且我遇到了一些问题,以获取一些用户信息。

我无法得到的信息如下:

hometown_location
current_location
work
下面是我的代码:
<?php
    $app_id = "";
    $app_secret = "";
    $my_url = "";
    session_start();
    $code = $_REQUEST["code"];
    if(empty($code)) {
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
          . $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=email,offline_access,publish_stream,user_birthday,user_education_history,user_location,user_hometown,user_relationships,user_relationship_details,user_work_history&state="
          . $_SESSION['state'];
        echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }
    if($_REQUEST['state'] == $_SESSION['state']) {
        $token_url = "https://graph.facebook.com/oauth/access_token?"
          . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
          . "&client_secret=" . $app_secret . "&code=" . $code;
        $response = file_get_contents($token_url);
        $params = null;
        parse_str($response, $params);
        $graph_url = "https://graph.facebook.com/me?access_token="
          . $params['access_token'];
        $user = json_decode(file_get_contents($graph_url));
        echo("Hello " . $user->name.'<br />');
        echo("id " . $user->id.'<br />');
        echo("mail  " . $user->email.'<br />');
        echo("birthday  " . $user->birthday.'<br />');
        echo("sex  " . $user->sex.'<br />');
        echo("hometown_location  " . $user->hometown_location.'<br />');
        echo("relationship_status  " . $user->relationship_status.'<br />');
        echo("current_location  " . $user->current_location.'<br />');
        echo("education  " . $user->education.'<br />');
        echo("work  " . $user->work.'<br />');
    }
    else {
    }
 ?>

我用"gender"来表示性别,但是我想不出其他的

您需要的属性是hometownlocation。Hometown要求用户授予user_hometown和Location要求用户授予user_location权限到您的应用程序:

  ....
  "username": "alienwebguy", 
  "hometown": {
    "id": "112276852118956", 
    "name": "Maple Grove, Minnesota"
  }, 
  "location": {
    "id": "102825596420583", 
    "name": "Antioch, California"
  }, 
  ....

根据Open Graph "User" API文档:

hometown    The user's hometown       user_hometown or friends_hometown   object containing name and id
location    The user's current city   user_location or friends_location   object containing name and id