繁体   English   中英

如何使用facebook graph api获取用户位置的名称(字符串)?

[英]How can I get the name (string) of the users location using the facebook graph api?

我不知道如何访问任何返回类型为“页面”(家乡或位置)的对象的数据。 我读了另一个stackoverflow问题,我需要使用'->'表示法来访问对象的特定部分,但这似乎不起作用。 任何帮助将是巨大的!

  $config = array(
    'appId' => '##############',
    'secret' => '#########################',
    'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
  );

  $facebook = new Facebook($config);
  $user_id = $facebook->getUser();
?>
<html>
  <head></head>
  <body>

  <?php
    if($user_id) {
      try {

        $user_profile = $facebook->api('/me','GET');
        echo "Name: " . $user_profile['name'] . "<br />";
        echo "Gender: : "  . $user_profile['gender'] . "<br />";
        echo "Birthday: " . $user_profile['birthday']. "<br />";
        echo "Religion: " . $user_profile['religion']. "<br />";
        echo "Hometown: " . $user_profile['hometown']->name. "<br />";
        echo "Location: " . $user_profile['location']->name. "<br />";

  } catch(FacebookApiException $e) {
    // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    $login_url = $facebook->getLoginUrl(array("scope" => "user_religion_politics,user_birthday"));
    echo 'Please <a href="' . $login_url . '">login.</a>';
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {

  // No user, print a link for the user to login
  $login_url = $facebook->getLoginUrl(array("scope" => "user_religion_politics,user_birthday,user_hometown,user_location"));
  echo 'Please <a href="' . $login_url . '">login.</a>';

}

?>

   </body>
</html>

代替

$user_profile['hometown']->name
$user_profile['location']->name

采用-

$user_profile['hometown']['name']
$user_profile['location']['name']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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