PHP / FaceBook API - 返回家乡和位置

时间:2012-09-29 16:05:10

标签: php facebook

我正在尝试从Facebook API存储家乡和位置,但我在从家乡和位置数组中提取数据时遇到问题。 (我已获得令牌许可)

"hometown": {
    "id": "210336945633449",
    "name": "xxxx, yyyyy"

我正在使用以下代码

function getUserData() {
        $fb_cookie = $this->getCookie();
        if($fb_cookie) {
            $url = 'https://graph.facebook.com/me?access_token='.$this->getAccessToken();
            $data = json_decode($this->getDataFromUrl($url));
            $fb['id'] = $data->id;
            $fb['name'] = $data->name;
            $fb['first_name'] = $data->first_name;
            $fb['last_name'] = $data->last_name;
            $fb['link'] = $data->link;
            $fb['birthday'] = $data->birthday;
            $fb['gender'] = $data->gender;
            $fb['email'] = $data->email;
            $fb['location'] = $data->location; // <-----
$fb['hometown'] = $data->hometown; // <-----
            $fb['timezone'] = $data->timezone;
            $fb['locale'] = $data->locale;
            $fb['updated_time'] = $data->updated_time;
            $fb['picture'] = 'http://graph.facebook.com/'.$data->id.'/picture';


            //tokens
            $fb['token'] = $fb_cookie['access_token'];
            $fb['token_expires'] = $fb_cookie['expires'];
            return $fb;
        }
    }

任何人都可以帮我解决这个问题吗?

由于

2 个答案:

答案 0 :(得分:0)

由于位置和家乡也是对象,您可以像这样使用这些值。

$fb['location'] = $data->location->name;
$fb['hometown'] = $data->hometown->name; 

答案 1 :(得分:0)

要获取位置名称,请尝试:

$loc=$data->getLocation();
$loc_name=$loc->getName();

上述代码适用于Facebook SDK v4。 (此SDK的PHP 5.4.0或更高版本是必需的)