How to get the current country of a Facebook user?

The Facebook API provides the user with the current location in two formats: name and identifier. The ID can be something like 104022926303756. The name can be something like Palo Alto, California or Beijing, China. From these two fields, how can I extract a country? All US locations are in the form of [City], [State] , while all non-US locations are in the form of [City], [Country] . Can I encode something less hacky than:

 $states = array( 'Alabama' 'Alaska', 'Arizona', // ... ); $country = 'USA'; if (!in_array($locationName, $states)) { preg_match('#, ([az]+$)#i', $locationName, $match); $country = $match[1]; } 
+4
source share
3 answers

How about this: http://developers.facebook.com/tools/explorer/?method=GET&path=fql%3Fq%3DSELECT% 20current_location% 20FROM% 20user% 20WHERE% 20uid% 3Dme ()

?

As you can see, using FQL in the user table, the JSON you get looks something like this:

 { "data": [ { "current_location": { "city": "Turin", "state": "Piemonte", "country": "Italy", "zip": "", "id": 115351801811432, "name": "Turin, Italy" } } ] } 

Do you have a field country, more readable :-)

EDIT: The link does not work, because there are no commands at the end of the query, in any case, the FQL query:

 SELECT current_location FROM user WHERE uid=me() 

"me ()" can be any user id.

+4
source

If you need more information, you can use latitude and longitude from https://graph.facebook.com/104022926303756 and pass them to the reverse geocoder.

http://maps.googleapis.com/maps/api/geocode/json?latlng=37.4293,-122.138&sensor=false

The Google API provides a lot of information, but can be quite useful.
A country code may be a better identifier than a country name, for example.
It is also possible to select the output language. Adding & lang = sv gives you "Kalifornien" as the name of the state, not "California".

+3
source

It is included in the signed request when they get into your application. This happens even when they still do not approve of your application.

0
source

Source: https://habr.com/ru/post/1399890/


All Articles