【问题标题】:How can I get the name (string) of the users location using the facebook graph api?如何使用 facebook graph api 获取用户位置的名称(字符串)?
【发布时间】:2014-04-13 21:12:23
【问题描述】:

我不知道如何访问返回类型为“页面”(家乡或位置)的对象的任何数据。 我阅读了另一个 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>

【问题讨论】:

    标签: php html facebook facebook-graph-api facebook-php-sdk


    【解决方案1】:

    代替

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

    使用-

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      相关资源
      最近更新 更多