3

我正在尝试使用新的 PHP SDK 和 GRAPH API 将旧的 FBML 应用程序迁移到 iFrame,但无法弄清楚 - 如何找到访问者的城市。

例如,在我自己的 Facebook 个人资料中,我列出了Current CityHometown

在此处输入图像描述

但是当我尝试下面的 iFrame 应用程序时,位置家乡没有打印出来,而包括我的雇主和教育在内的其他数据被打印出来:

<?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);
    }
}

?>

这是我自己看到的部分数据,当前位置不存在:

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
...........
)

问候亚历克斯

4

2 回答 2

5

您只需要user_locationanduser_hometown 权限

所以你的登录网址应该是这样的:

$url = $facebook->getLoginUrl(array(
    'scope' => 'user_location,user_hometown'
));
于 2011-05-06T17:27:52.927 回答
2

这并不难,这是您调用 fb api 时所得到的(来自 FB 文档中示例的代码的一部分)

   "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"
   },

要获取您首先调用 api 的信息

$me = $facebook->api('/me');

正如您在示例中看到的,家乡的名称位于“家乡”数组中。位置(当前城市)也是如此,它位于“位置”数组中。所以你现在要做的是

$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'];

于 2011-05-06T12:38:09.813 回答