通过PHP 5.0 SDK获取facebook生日

时间:2016-06-08 09:09:44

标签: facebook-graph-api facebook-php-sdk

我无法获取我的代码来获取我的测试帐户的“生日”,甚至是我自己(在任何地方都没有成功!)回应虽然从我所看到的我正确地请求它并且我拥有适当的权限为“user_birthday”。

但是facebook正在回送该字段的对象。

所有其他字段都可以完美地运行,就像我在结果页面上看到的那样。

请求文件的相关部分

的login.php

$fb = new Facebook\Facebook([ 'app_id' => 'my_actual_app_id', 'app_secret' => 'my_actual_secret_code', 'default_graph_version' => 'v2.3', ]); $helper = $fb->getRedirectLoginHelper(); $permissions = ['email', 'user_birthday']; $loginUrl = $helper->getLoginUrl('my_actual_url', $permissions);

回调文件

登录-callback.php

`$fb = new Facebook\Facebook([
                'app_id' => 'my_actual_id',
                'app_secret' => 'my_actual_secret_code',
                'default_graph_version' => 'v2.3',
        ]);
$helper = $fb->getRedirectLoginHelper();
try {
    $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}
if (isset($accessToken)) {
    // Logged in!
    // OAuth 2.0 client handler
    echo "<br><br><br> Ok 1 <br>";
    $oAuth2Client = $fb->getOAuth2Client();
    echo "Ok 2 <br>";
    // Exchanges a short-lived access token for a long-lived one
    $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken(strval($accessToken));
    echo "Ok 3 <br>";
    $_SESSION['login'] = $longLivedAccessToken;
    echo "Ok 4 <br>";
    $fb->setDefaultAccessToken($longLivedAccessToken);
    $response = $fb->get('/me?locale=en_US&fields=id,last_name,first_name,email,birthday');
    //$profile = $response->getGraphNode()->asArray();
    echo "Ok 5 <br>";
    // get response
    echo "Ok 6 <br>";
    $node = $response->getGraphNode();
    echo "Ok 7 <br>";
    $fuid = $node->getField('id');              // To Get Facebook ID
    echo "Ok 8 $fuid <br>";
    $fblastname = $node->getField('last_name');
    echo "Ok 9 $fblastname <br>";
    $fbfirstname = $node->getField('first_name');// To Get Facebook first name
    echo "Ok 10 $fbfirstname <br>";
    $femail = $node->getField('email');    // To Get Facebook email ID
    echo "Ok 11 $femail <br>";
    $fbirthday = $node->getField('birthday');
    echo "<br>";
    var_dump($fbirthday);
    echo "OK 12 $fbirthday <br>";
    /* ---- Session Variables -----*/
    $_SESSION['FBID'] = $fuid;
    $_SESSION['LASTNAME'] = $fblastname;
    $_SESSION['FIRSTNAME'] = $fbfirstname;
    $_SESSION['EMAIL'] =  $femail;
    $test = "perfect";
    // = Db::getInstance()->checkuser($fuid,$fblastname,$fbfirstname,$femail);
    echo "<br><br><br> Ok 13 $test : Wait for refresh <br>";
    // Now you can redirect to another page and use the
    // access token from $_SESSION['facebook_access_token']
}
//header ( "refresh:15; url=index.php?action=map");`

我的login-callback.php代码

的结果页面
`Ok 1 
Ok 2 
Ok 3 
Ok 4 
Ok 5 
Ok 6 
Ok 7 
Ok 8 104975603177961 
Ok 9 Teck 
Ok 10 Pierre 
Ok 11 myemail@gmail.com 

object(Facebook\GraphNodes\Birthday)#21 (5) { ["hasDate":"Facebook\GraphNodes\Birthday":private]=> bool(true) ["hasYear":"Facebook\GraphNodes\Birthday":private]=> bool(true) ["date"]=> string(26) "1955-05-17 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Paris" }`

我已经尝试将它转换为数组并要求$ array_var ['birthday'],但结果相同......(但它适用于所有其他字段)

有一些方法可以获取我的生日信息,而那里,但我在facebook文档中找不到任何帮助我的信息。

1 个答案:

答案 0 :(得分:5)

使用getDecodedBody()。它将响应的已解析主体作为数组返回。

$response = $fb->get('/me?locale=en_US&fields=id,last_name,first_name,email,birthday');
$userData = $response->getDecodedBody();
echo $userData['birthday'];
OR
var_dump($userData);

Facebook\FacebookResponse