PHP Facebook API - 无法从API获得生日

时间:2012-09-05 10:27:20

标签: php facebook api facebook-apps

我有一个基于此身份验证代码的非常简单的Facebook应用程序:

$session = $facebook->getUser();

if (!$session) {
    $url = $facebook->getLoginUrl(array(
               'canvas' => 1,
               'fbconnect' => 0,
               'scope' => 'publish_stream, email, user_activities, user_birthday, user_hometown'
               //'req_perms' => 'publish_stream,email,user_location,user_birthday,user_hometown'
           ));
    echo "<script type='text/javascript'>top.location.href = 'FB_URL';</script>";
} else {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        echo "<script type='text/javascript'>top.location.href = 'FB_URL';</script>";
    exit;
    } 
}

echo "\n === \n";
print_r($uid);
echo "\n === \n";
print_r($me);

输出结果为:

=== ID_VALUE === Array ( [id] => ID_VALUE [name] => NAME [first_name] => FIRST_NAME [last_name] => LAST_NAME [link] => PROFILE_URL [username] => USERNAME [gender] => male [email] => EMAIL [timezone] => 2 [locale] => en_US [verified] => 1 [updated_time] => 2012-05-16T13:00:04+0000 )

我希望在指定扩展权限user_birthdayuser_hometown后,我应该在数组中获取有关用户生日和家乡的信息......但不幸的是,这些信息在那里丢失了。

我错过了什么或者我做错了什么?

1 个答案:

答案 0 :(得分:4)

我做了一些调查,

你可以试试这个:$me = $facebook->api('/me?fields=birthday');

作为范围 - 您请求权限,user_birthday

确保您的$facebook对象有access_token

看看并报告回来,我会尽力帮助。

编辑: 我不明白你从哪里得到FB_URL,因为你在$url变量中写了LoginUrl 我查看了您的代码并建议您尝试这种方式:

if (!$session) {
    $url = $facebook->getLoginUrl(array(
              'redirect_uri' => //SET back to your page or whenever you need
               'scope' => 'publish_stream, email, user_activities, user_birthday, user_hometown'          
           ));
    echo '<script type="text/javascript">top.location.href =' . $url . '</script>';
} else {
    try {           
        $me = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        echo $e->getMessage();       
    } 
}

另外验证用户访问令牌的所有权限 获取$ facebook-&gt; getAccessToken();和调试在这里 - &gt; Token debugger