$ facebook-> getUser()始终返回零

时间:2011-09-23 11:15:39

标签: php facebook facebook-graph-api

您好我在我的网站上集成了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总是为零。为什么会这样。请帮忙..

2 个答案:

答案 0 :(得分: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;   
}

答案 1 :(得分: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>