显示Facebook用户的性别和其他人

时间:2011-10-09 06:25:34

标签: php facebook-graph-api

我正在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 {
    }
 ?>

我与“性别”发生了性关系,但我无法弄明白其他人。

1 个答案:

答案 0 :(得分:3)

您想要的属性是hometownlocation。 Hometown要求用户授予user_hometown,并且位置要求用户向您的应用授予user_location权限:

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

根据Open Graph "User" API docs

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