0

Hello i'm struggling with getting the location of an user when he logs on the website. I want to get city and country. Here is my code.

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
        ));
}

1 Answers1

0

You can call the user's location : $user_location = $facebook->api('/'.$fbid.'?fields=location');

The output is an array (id and name of the location). Both city and country has its own id. And there is no valid structure for user's location. Users can only put his city/country alone or both. You have to manipulate the array so you can get the output based on what you expect.

Hope it can help.

King Goeks
  • 492
  • 4
  • 11
  • 28
  • 2
    oh i see why it's not working for country, prolly they didnt complete theyr country, here is the code i used and it's working it's displaying city but not country.`$facebook_location = $user_profile['location']; $facebook_city_country = $facebook_location['name']; $city_country=explode(',',$facebook_city_country); $city=$city_country[0]; $country=$city_country[1];` – Sima Cristian Alexandru Oct 28 '13 at 11:37
  • 2
    as I said, maybe the current user didn't put his country on his profile. You can check it on graph api explorer tools. Here is the example of my friend's locations. You can see that the location can only a city or a country or both. http://pastebin.com/RJQmXaKP – King Goeks Oct 28 '13 at 11:56