2011-09-23 103 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>