2011-09-23 70 views
0

嗨,我在我的網站上集成了facebook連接功能。所以我使用了facebook新圖api。我用下面的代碼..

............. 
............. 
    $facebook = new Facebook(array(
    'appId' => $fbconfig['appid'], 
    'secret' => $fbconfig['secret'], 
)); 

$uid = $facebook->getUser(); 
var_dump($uid); 
if($uid) 
{ 
    //do someting 
} 
else 
...... 

但問題是$ uid總是爲零。爲什麼發生這種情況。請幫助..

+0

我覺得這可能重複。請檢查[我的Facebook對象爲空] [1]。 [1]:http://stackoverflow.com/questions/7309684/my-facebook-object-is-empty –

+0

不過我m到處相同的錯誤。有什麼解決方案,爲什麼發生這種情況。 – user392406

+0

[爲什麼Facebook PHP SDK getUser總是返回0?](http://facebook.stackoverflow.com/questions/6790272/why-is-facebook-php-sdk-getuser-always-returning-0) – bkaid

回答

0

嘗試這個

$user = null; 
$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
)); 

$user = $facebook->getUser(); 

if ($user) { 
echo "user"; 



} 
else 
{ 

echo("<script language=\"javascript\">"); 
echo("top.location.href = \"http://apps.facebook.com/myapp\";"); 
echo("</script>"); 
exit; 
} 
+0

看看https://github.com/facebook/php-sdk/issues/418 –

0

這是代碼,通過它可以使用FB帳號登錄..

<?php 
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);   
if(isset($cookie)) 
{ 
    $first_name = json_decode(@file_get_contents('https://graph.facebook.com/me?access_token=' .$cookie['access_token']))->first_name; 
    $last_name = json_decode(@file_get_contents('https://graph.facebook.com/me?access_token=' .$cookie['access_token']))->last_name; 
} 
?> 
<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script> 
      FB.init({appId: <?php echo FACEBOOK_APP_ID;?>, status: true, cookie: true, xfbml: true}); 
      FB.Event.subscribe('auth.logout', function(response) {}); 
      FB.Event.subscribe('auth.sessionChange', function(response) { 
      if (response.session) { 

       FB.api('/me', function(response) {    
       if(response.id!='undefined') 
       { 
        window.location='Fbaccess.php?first_name='+response.first_name+'&last_name='+response.last_name+'&email='+response.email; 
       } 
       else 
       { 
        window.location='login.php'; 
       } 
       }); 

      } else { 

       // The user has logged out, and the cookie has been cleared 
      } 
      }); 

     </script>