1

我有樣本Facebook登錄的實現,只是意識到它停止工作:Facebook的連接:JavaScript的登錄會話,但PHP不

<?php 
// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'myappid', 
    'secret' => 'mybigsecreet', 
    'cookie' => true, 
)); 

// We may or may not have this data based on a $_GET or $_COOKIE based session. 
// 
// If we get a session here, it means we found a correctly signed session using 
// the Application Secret only Facebook and the Application know. We dont know 
// if it is still valid until we make an API call using the session. A session 
// can become invalid if it has already expired (should not be getting the 
// session back in this case) or if the user logged out of Facebook. 
$session = $facebook->getSession(); 

$me = null; 
// Session based API call. 
if ($session) { 
    try { 
    $uid = $facebook->getUser(); 
    $me = $facebook->api('/me'); 

     $r = new registro_usuarios(); 
     $r->facebook($uid,$me['name'],'https://graph.facebook.com/'.$me['id'].'/picture'); 

     echo '----------------------------'.$me; 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    echo '----------------------------'.$e; 
    } 
}else echo 'nosession'; 
echo $session; 

打印nosesion但是當我點擊登錄按鈕(臉譜)螢火日誌:FB.login() called when user is already connected.和登錄彈出(臉書)不會打開。

我錯過了什麼? facebook-api一年後過時了嗎?

回答

2

$facebook->getSession()不再工作。這就是爲什麼它可能會從第一循環退出。

$會話= $ facebook->的getSession();

$ session中始終爲空。

您可以通過$facebook->getUser()檢查SEESION如果它給0,則沒有活動的會話否則你會得到會話用戶的Facebook的帳號。

看到這個鏈接也可能會得到更多的inforamation:

+0

非常感謝。會嘗試讓你知道。他們不應該重新映射這個功能嗎? – 2012-03-23 08:14:20

+0

您可以通過'$ facebook-> getUser()'檢查會話。如果它給0,那麼沒有活動會話。這就是爲什麼'$ facebook-> getSession()'函數被棄用的原因。沒有必要映射功能與另一個。 – 2012-03-23 08:54:58

+0

所以我需要再次下載SDK的權利? – 2012-03-23 11:03:14

1

你應該下載從GitHub最新的SDK在https://github.com/facebook/php-sdk

然後執行以下操作和用戶會話將被記錄。

require '../src/facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => '344617158898614', 
    'secret' => '6dc8ac871858b34798bc2488200e503d', 
)); 

// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl(); 
} 

這取自示例代碼。對我來說工作得很好。