Facebook Graph API - how to get user country?

I'm developing a Facebook application using their Graph API. I have previously developed an app using the deprecated REST API.

I've come across a problem in that I can't retrieve the user's country, only their 'location' - which is just the city and state / region in a combined format (such as 'Houston, Texas').

How is it possible to retrieve the user's country using the Graph API? If this isn't directly possible, what workarounds might there be to achieve this?


Solution 1:

https://api.facebook.com/method/fql.query?format=json&query=SELECT%20current_location%20FROM%20user%20WHERE%20uid=4&access_token=<ACCESS_TOKEN>

[
  {
    "current_location": {
      "city": "Palo Alto",
      "state": "California",
      "country": "United States",
      "zip": "",
      "latitude": 37.4441,
      "longitude": -122.163,
      "id": 104022926303756,
      "name": "Palo Alto, California"
    }
  }
]

FQL Documentation

Solution 2:

One solution I have found, which is merely a workaround, is to use the GeoNames API - http://www.geonames.org/export/web-services.html

Using something like:

http://api.geonames.org/search?q=houston%2C%20texas&featureClass=P&username=myusername

Returns results in an xml object, with the country as part of the data.

I'm not particularly happy with this as a solution however, as it adds an unnecessary layer of complications on to my application - plus requests to this free services are limited to 2000 per hour and 30,000 per day.

Solution 3:

Use the php sdk:

require 'facebook.php';

$facebook = new Facebook(array( 'appId' => 'APPID', 'secret' => 'APPSECRET',
'cookie' => true, ));

Then ask for a signed request:

  $signed_request = $facebook->getSignedRequest();
  $country = $signed_request["user"]["country"];

Now $country has a string such as "us" for United States and "uk" for United Kingdom etc.

Solution 4:

That is a good question.

The problem is that any application run under facebook has to go through facebook servers - fb proxy and because of this the IP you get is not that of visitors but facebook's servers' instead which is of no use.

I have not seen any solution for this yet except for getting user's country from his profile which again is only possible if user had made it public or authorized the permission for it.

Here is useful link you might want to check out there too:

  • http://fbexchange.net/