Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API - how to get user country?

Tags:

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?

like image 814
BrynJ Avatar asked Feb 28 '11 14:02

BrynJ


People also ask

How do I find Facebook user information?

You can review your information on Facebook (such as recent activity) from the Your Facebook Information section of your Facebook Settings. Tap in the top right of Facebook. Scroll down and tap Settings. Scroll down to Your Facebook Information and tap the information you want to review.

How do I get Facebook Page Insights on graph API?

You can access Page Insights by typing /insights after the name of your page in the URL field. This command will retrieve all of the Insights associated with your Page. Type "/insights" after your company name. Click the Submit button.

How do I use Facebook Graph API and extract data?

To use the Graph API Explorer tool, go to developers.facebook.com/tools/explorer. Generate the access token you need to extract data from the Facebook Graph API by selecting Get User Access Token from the App Token drop-down menu under the name of your app.


4 Answers

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.

like image 38
BrynJ Avatar answered Nov 04 '22 05:11

BrynJ


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

like image 172
Valerchik Avatar answered Nov 04 '22 07:11

Valerchik


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.

like image 44
Tom Avatar answered Nov 04 '22 07:11

Tom


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/
like image 28
Sarfraz Avatar answered Nov 04 '22 06:11

Sarfraz