Facebook iFrame app - how to find user's current city?
I'm trying to migrate an old FBML app to iFrame using the new PHP SDK and GRAPH API, but cannot figure out - how to find the visitor's city.
For example in my own Facebook profile I 开发者_运维百科list both Current City and Hometown:
But when I try the following iFrame app, the location and hometown are not printed, while other data including my employers and education is printed:
<?php
include_once 'facebook.php';
$facebook = new Facebook(array(
'appId' => "182820975103876",
'secret' => "XXXXXXX",
'cookie' => true,
));
$session = $facebook->getSession();
if (!$session) {
$url = $facebook->getLoginUrl();
print("<script type='text/javascript'>top.location.href = '$url';</script>");
} else {
try {
$me = $facebook->api('/me');
print('<pre>');
print_r($me);
print('</pre>');
} catch (FacebookApiException $e) {
print("Error:" . $e);
}
}
?>
Here is part of the data I see for myself, the current location isn't there:
Array
(
[first_name] => Alexander
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 106509962720287
[name] => Riga Nr. 40
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 103130426393616
[name] => RWTH Aachen University
)
[year] => Array
(
[id] => 143018465715205
[name] => 2000
)
[type] => College
)
)
[gender] => male
...........
)
Regards Alex
You just need the user_location
and user_hometown
permissions.
So your login url should read something like:
$url = $facebook->getLoginUrl(array(
'scope' => 'user_location,user_hometown'
));
thats not too hard here is what you are getting when you call fb api ( part of a code from examples in FB docs)
"id": "220439",
"name": "Bret Taylor",
"first_name": "Bret",
"last_name": "Taylor",
"link": "http://www.facebook.com/btaylor",
"username": "btaylor",
"hometown": {
"id": "108363292521622",
"name": "Oakland, California"
},
"location": {
"id": "109650795719651",
"name": "Los Gatos, California"
},
to get that info you call api first
$me = $facebook->api('/me');
as you see in the example that the name of a hometown is inside an "hometown" array. same goes for location (current city) its inside "location" array.. so what you do now is
$hometown = $me['hometown']['name'];
$current_city = $me['location']['name'];
echo 'You are currently living in: '.$current_city.' but your hometown is: '.$hometown
hope it helps man ;)
EDIT:
$access_token = $facebook->getAccessToken(); $req_id = $_GET['request_ids']; $req = $facebook->api('/me/?access_token='.$access_token); echo $req['hometown']['name'];
精彩评论