Facebook Graph API: Get user_location
Solution 1
Your friend is considered a user, so you go to the user documentation, get the field name location
(to be send in the fields
parameter) AND you check if you need a specific permission:
Requires
user_location
orfriend_location
permission
HOWEVER there's a typo! to get your friend's location you need the friends_location
(with s), reference.
Here's a PHP-SDK example:
$facebook->api('/friend_id', 'get', array("fields"=>"name,location"));
Here I'm retrieving the name
and location
fields (please note that the id
field will be retrieved automatically).
Solution 2
FB.api('/me', function(response) {
var location = response.location.name;
});
Solution 3
The fastest way to get all the information you want from friends of a user you can use the FQL method:
$this->api->api(array('method'=>'fql.query','query'=>"SELECT uid,name,first_name,middle_name,last_name,pic_square,hometown_location,current_location,profile_url,email,website FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"));
For a list of all the tables with their corresponding column names I refer to: Facebook FQL Reference
Solution 4
The FaceBook Documentation has the information you're looking for, including sample code and instructions. It's available at:
http://developers.facebook.com/docs/guides/canvas
You may also find the Graph API useful: http://developers.facebook.com/docs/api
Solution 5
https://graph.facebook.com/me/friends?access_token="get this from fb tool"&fields=location
Related videos on Youtube
Anush Shetty
Updated on July 01, 2020Comments
-
Anush Shetty almost 4 years
Is there a way to get the location of my facebook friends using the Graph API?
-
Josh over 13 yearsas a little hint, typically, you'll want to answer with more than just a link. Including sample code and detailed instructions are great ways to produce high quality answers. I edited your answer just a little bit to help you out.
-
grm about 13 yearsDoes this work for you? I really can't get it to work. Only name is returned (asking for perms correctly with friendS_location as well).
-
ifaour about 13 years@grm: yes it does BUT your friend should have set the city to appear! I found that 40% of my friends didn't set it!
-
Dan H over 11 yearsRemember that the location field returns an object. In order to retrieve the location you have to use $profile['location']->name.