簡體   English   中英

PHP SDK 3.1.1 getUser()有時會返回0

[英]PHP SDK 3.1.1 getUser() sometimes return 0

這讓我瘋狂> =(

$ facebook-> getUser()有時運行良好,但有時會返回0

這是我的代碼: require 'fbapi/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
require 'fbapi/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();

appIdsecret是正確的。

這可能是getUser有時會返回0的原因???

你只需要輸入php標題

header('P3P: CP="NOI ADM DEV COM NAV OUR STP"');

這很可能是Facebook的結果(開發人員在一段時間之前已經完成了這個)。 如果$user返回null或0,只需將它們重新路由到登錄URL(您應該擁有)。 它再次返回0的可能性很小(除非他們的結尾有一個錯誤,或者你的代碼比你發布的更多)。

我遇到了類似的問題。 $ facebook-> getUser()返回0,有時當用戶實際沒有登錄時它返回了有效的用戶ID,當我嘗試進行圖形api調用時導致Fatal Oauth錯誤。 我終於解決了這個問題。 我不知道它是否是正確的方式,但它的工作原理。 這是代碼:

<?php
include 'includes/php/facebook.php';
$app_id = "APP_ID";
$app_secret = "SECRET_KEY";
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
    'cookie' => true
));

$user = $facebook->getUser();

if ($user <> '0' && $user <> '') { /*if valid user id i.e. neither 0 nor blank nor null*/
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) { /*sometimes it shows user id even if user in not logged in and it results in Oauth exception. In this case we will set it back to 0.*/
error_log($e);
$user = '0';
}
}
if ($user <> '0' && $user <> '') { /*So now we will have a valid user id with a valid oauth access token and so the code will work fine.*/
echo "UserId : " . $user;

$params = array( 'next' => 'http://www.quickbite.co.in' );
echo "<p><a href='". $facebook->getLogoutUrl($params) . "'>Logout</a>";

$user_profile = $facebook->api('/me');
echo "<p>Name : " . $user_profile['name'];
echo "<p>";
print_r($user_profile);

} else {/*If user id isn't present just redirect it to login url*/
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'email,offline_access'))}");
}

?>

所以我正在努力解決同樣的問題, getUser()總是返回0.完全沒有任何意義。 NginX錯誤日志顯示以下錯誤'CSRF狀態令牌與提供的不匹配'。 然后我開始調試sdk並將其縮小到getAccessTokenFromCode()無法從Facebook服務器獲得正確的accessstoken,這與SSL證書有關。

下面提到的鏈接的解決方案為我解決了問題。

https://github.com/facebook/php-sdk/issues/272

此外,您可能需要將與sdk捆綁在一起的'fb_ca_chain_bundle.crt'文件復制到您的facebook類文件所在的文件夾中。 我做到了。

我希望這是有道理的。

我找到了一個解決這個問題的簡單方法..當facebook類無法在您的服務器上設置或添加會話變量時會發生這種情況。

在我的代碼中,我在包含facebook類之前使用了session_write_close()因為我得到了這個錯誤..“CSRF狀態令牌與提供的不匹配。”..

然后我在代碼的末尾移動了session_write_close(),最后它工作了.. :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM