5

I am trying to retrieve Zip code of a user using facebook graph API. I am using follow code for that. Code is in php.

$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            ));

$user = $facebook->getUser();
//$login_url = $facebook->getLoginUrl(array( 'scope' => 'email'));
$login_url   = $facebook->getLoginUrl(
           array(
               'scope'         => 'email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown'
           )
   );
//get user basic description
//$userInfo           = $facebook->api("/$user");

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }

Here I am getting the $user_profile array, It has all information other that zip code. But I gave permission for that. Is there any way to get zip code from facebook.

Amar Banerjee
  • 4,992
  • 5
  • 34
  • 51

2 Answers2

4

If you check the documentation for the /me call at https://developers.facebook.com/docs/reference/api/user/ you'll note that there is no reference to the user's zip or postal code.

I'd recommend you use ask for the user_location permission and then call /me?fields=location and try to derive the zip code from the information contained therein - this answer https://stackoverflow.com/a/7129344/2091159 - has some information about doing so.

Community
  • 1
  • 1
Reuben Thompson
  • 351
  • 5
  • 10
  • You simply can't get the zip code from only a city name. Many cities have a lot of zip codes. And the worst thing is not this, but realizing not all databases have the location names with the same format, and search by string is painful, and even worse, there are repeated location names all spread any country. Your best chance is to ask directly to the user, after the login is done. Search this way is not recommended. I've worked with these issues, and you are gonna get crazy if you want to this on different countries. – appartisan Jun 10 '16 at 00:41
  • See the link supplied – Reuben Thompson Jul 03 '16 at 16:39
  • I think that link doesn't solve the problem, @Reuben Thompson. If you get 'Madrid, Spain' from the location given by facebook API, you cannot get a single zip code, as Madrid city has about 63 different zip codes. Even worse, you are not sure the user is in Madrid ( Madrid Region ) as there are other 'Madrid' cities in the country, and facebook location doesn't provide this information. Finally, searching for textual coincidences in town names is not normally an option, as different databases save different versions ( Los Angeles - Los Angeles City - Angeles, Los )... – appartisan Jul 04 '16 at 17:37
-1

This is the JavaScript equivalent of what you want, I think:

FB.api('/me', function(user){
  FB.api('/' + user.location.id, function(location){
    alert(location.location.zip);
  });
})
sites
  • 21,417
  • 17
  • 87
  • 146