Search code examples
facebookiframefacebook-graph-apilocationfacebook-iframe

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:

enter image description here

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


Solution

  • 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'
    ));