如何使用PHP SDK获取Facebook国家和城市


how to get Facebook country and city using php sdk

您好,我正在努力获取用户登录网站时的位置。我想得到城市和国家。这是我的代码。

require 'src/facebook.php';  // Include facebook SDK file
  // Include functions
$facebook = new Facebook(array(
  'appId'  => '446287188809548',   // Facebook App ID 
  'secret' => '5210aae00c4f11eca8451a9da9be4aaf',  // Facebook App Secret
  'cookie' => true, 
));
$user = $facebook->getUser();
if ($user) {
  try {
    $user_profile = $facebook->api('/me');
        $fbid = $user_profile['id'];                 // To Get Facebook ID
        $fbuname = $user_profile['username'];  // To Get Facebook Username
        $fbfullname = $user_profile['name']; // To Get Facebook full name
          $femail = $user_profile['email'];    // To Get Facebook email ID
    //       checkuser($fbid,$fbuname,$fbfullname,$femail);    // To update local DB
  } catch (FacebookApiException $e) {
    error_log($e);
   $user = null;
  }
}
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl(array(
         'next' => 'http://websoftit.ro/lackoflove/logout.php',  // Logout URL full path
        ));
} else {
 $loginUrl = $facebook->getLoginUrl(array(
        'scope'     => 'email', // Permissions to request from the user
        ));
}

您可以调用用户的位置: $user_location = $facebook->api('/'.$fbid.'?fields=location');

输出是一个数组(位置的 id 和名称)。城市和国家都有自己的ID。并且用户的位置没有有效的结构。用户只能单独放置他的城市/国家或两者兼而有之。您必须操作数组,以便可以根据预期获得输出。

希望它能有所帮助。