2012-01-18 41 views
1

我正在尝试在我的网站上集成facebook连接。一旦我登录Facebook并订阅,事件将触发并且其response.status返回“连接”。然后我重新加载页面,但我没有得到任何用户数据。如果我手动刷新页面,则只能做我的用户信息获取登录需要重新加载页面2次才能获得用户ID和信息

我的代码是:

$user = $facebook->getUser(); 
if ($user) { 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); 
     //print htmlspecialchars(print_r($user_profile, true)); 
    } catch (FacebookApiException $e) { 
     echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
     $user = null; 
    } 
} 
    <div id="fb-root"></div> 
    <?php if ($user) { ?> 
     Your user profile is 
     <pre> 
     <?php print htmlspecialchars(print_r($user_profile, true)) ?> 
     </pre> 
    <?php } //else { ?> 
     <fb:login-button></fb:login-button> 
    <?php //} ?> 
    <script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
     appId  : 'YOUR_APP_ID', 
     status  : true, 
     cookie  : true, 
     xfbml  : true, 
     oauth  : true, 
     }); 
    FB.Event.subscribe('auth.login', function(response) { 
     alert("status " + response.status); 
     alert('reload'); 
     window.location.reload(); 
    } 
    }; 
    (function(d){ 
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     d.getElementsByTagName('head')[0].appendChild(js); 
    }(document)); 
    </script> 

请帮我解决这个

回答

2

也许你刷新页面太快(所以cookie尚未由Facebook JS-SDK设置)。

为了确保cookie设置正确,您可以使用这样的事情:

FB.Event.subscribe('auth.login', function(response) { 
    if (response.status == 'connected'){ 
    FB.getLoginStatus(function(){ 
     window.location.reload(); 
    }); 
    } 
}) 
相关问题