Facebook API获取位置信息

时间:2017-04-19 16:30:20

标签: php facebook facebook-graph-api

我正在使用Facebook PHP API V2.9并想要获取用户的位置city,country_code,latitude等。 该应用程序的权限由Facebook授予。这就是代码的外观:

$fb->setDefaultAccessToken($_SESSION['fb_access_token']);
$response = $fb->get('/me?fields=first_name,last_name,picture,email,gender,location');
$userNode = $response->getGraphUser();

$fb_user_first_name     = $userNode->getField('first_name');
$fb_user_last_name      = $userNode->getField('last_name');
$fb_user_gender         = $userNode->getField('gender');
$fb_user_picture        = 'https://graph.facebook.com/'.$fb_user_id.'/picture?type=large'; #$userNode->getField('picture')['url'];
$fb_user_email          = $userNode->getField('email');
$fb_user_city           = $userNode->getField('location');
$fb_user_country        = '';
$fb_user_country_code   = '';

// Get City
if(!empty($fb_user_city)) {
    $city_response  = $fb->get('/'.$fb_user_city['id'].'/locations?fields=city,country,country_code,latitude,longitude');
    $cityNode       = $city_response->getGraphObject();
}

但是,// Get City部分以错误结束:

  

致命错误:未捕获的异常   'Facebook \ Exceptions \ FacebookSDKException',消息'无法使用   将响应从Graph转换为GraphNode,因为响应看起来   像GraphEdge一样。尝试使用GraphNodeFactory :: makeGraphEdge()   代替。' in /libs/Facebook/GraphNodes/GraphNodeFactory.php:224 Stack   跟踪:#0 /libs/Facebook/GraphNodes/GraphNodeFactory.php(93):   Facebook的\ GraphNodes \ GraphNodeFactory-> validateResponseCastableAsGraphNode()

     

1 /libs/Facebook/FacebookResponse.php(289):Facebook \ GraphNodes \ GraphNodeFactory-> makeGraphNode(NULL)#2

     

/libs/Facebook/FacebookResponse.php(273):   Facebook \ FacebookResponse-> getGraphNode(NULL)#3   /oauth/callback.facebook.php(129):   Facebook \ FacebookResponse-> getGraphObject()#4 {main}引入   第224行/libs/Facebook/GraphNodes/GraphNodeFactory.php

问题是,为什么?我按照https://developers.facebook.com/docs/graph-api/reference/location/的说明进行操作但不起作用......

0 个答案:

没有答案