简体   繁体   English

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

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

I can not figure out how to access data of anything that returns an object with type 'page' (hometown or location). 我不知道如何访问任何返回类型为“页面”(家乡或位置)的对象的数据。 I read on another stackoverflow question that I needed to use the ' -> ' notation in order to access specific pieces of the object but that doesn't seem to be working. 我读了另一个stackoverflow问题,我需要使用'->'表示法来访问对象的特定部分,但这似乎不起作用。 Any help would be great! 任何帮助将是巨大的!

  $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>

Instead of 代替

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

Use- 采用-

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

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

相关问题 如何使用某些条件(例如Graph API中的位置)在Facebook中搜索用户 - How do I search for users in Facebook using certain conditions such as location in Graph API 根据名称和位置Facebook Graph API,FQL搜索用户 - Search users on the basis of name and location Facebook Graph API, FQL 如何使用Facebook PHP图形API获取用户图片? - How do I get the users picture using the Facebook PHP graph API? 如何使用facebook graph api显示用户个人资料图片? - How can I display the users profile pic using the facebook graph api? Facebook Graph API,如何获取用户电子邮件? - Facebook Graph API, how to get users email? 卡在Graph API(facebook API)上,如何使用此代码获取姓名,电子邮件和性别? - Stuck on Graph API (facebook API), how do i get name, email and sex using this code? 如何使用Graph API分别获取每个Facebook页面的名称? - How can I get the name of every Facebook page separately with the Graph API? Facebook Graph API:使用App访问令牌,可以获取具有特定名称的用户吗? - Facebook Graph API: With an App access token, can I obtain users with a certain name? 如何使用facebook ID从facebook graph API获取用户位置和家乡 - How to get a user location and hometown from facebook graph API using facebook ID Facebook Graph API:我可以通过graph api获得多少Facebook点赞? - Facebook Graph API: How many facebook likes can I get for graph api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM