2016-06-08 77 views
2

我無法讓我的代碼得到我的測試帳戶的'生日',甚至我自己的(任何地方都沒有成功!)來回顯,雖然從我看到我要求它正確,我有適當的權限「user_birthday」。通過PHP 5.0 SDK獲取Facebook生日

但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 [email protected] 

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 [「生日」]但有相同的結果...

有(與所有雖然其他領域的文章)某種方式來獲取我的生日信息,而它是那裏,但我沒有找到任何在Facebook文檔來幫助我。

回答

3

使用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

+0

這種簡單的修復完全適用於所有的領域,謝謝!除了從'var_dump($ response)'自己來判斷這個,有沒有關於這種方法在互聯網上的任何信息或文檔,我應該找到? –

+0

向Graph API發送請求後,響應將以Facebook \ FacebookResponse實體的形式返回。 [FacebookResponse](https://developers.facebook.com/docs/php/FacebookResponse/5.0.0) – Nadeem0035

+0

謝謝陌生人! –