2012-12-18 210 views
1

我正在使用用PHP编写的Facebook API连接脚本(由Facebook提供)。一切正常,它会生成登录URL并在我登录时将我重定向回网站。Facebook API:登录,但仍不起作用

但是,$ user变量似乎未定义。

有没有人遇到过类似的问题?在Facebook应用程序统计信息页面中,我可以看到该应用程序已被使用。

更新 - 代码:

<?php 

$app_id  = "xxxxxxxxxxxxxx"; 
$app_secret = "xxxxxxxxxxxxx"; 
$site_url = "http://xxx"; 

try{ 
    include_once "src/facebook.php"; 
}catch(Exception $e){ 
    error_log($e); 
} 

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

if($user = $facebook->getUser()) 
{ 
    echo 'ok'; 
} 
else 
{ 
} 

if($user){ 
//==================== Single query method ====================================== 
    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; 
    } 
//==================== Single query method ends ================================= 
} 

if($user){ 
    // Get logout URL 
    $logoutUrl = $facebook->getLogoutUrl(); 
}else{ 
    // Get login URL 
    $loginUrl = $facebook->getLoginUrl(array(
     'scope'  => 'email,user_birthday,user_about_me', 
     'redirect_uri' => $site_url . '/auth_complete.php', 
     )); 
} 

if($user){ 
    // Proceed knowing you have a logged in user who has a valid session. 

//========= Batch requests over the Facebook Graph API using the PHP-SDK ======== 
    // Save your method calls into an array 
    $queries = array(
     array('method' => 'GET', 'relative_url' => '/'.$user), 
     array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'), 
     array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'), 
     array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'), 
     ); 

    // POST your queries to the batch endpoint on the graph. 
    try{ 
     $batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST'); 
    }catch(Exception $o){ 
     error_log($o); 
    } 

    //Return values are indexed in order of the original array, content is in ['body'] as a JSON 
    //string. Decode for use as a PHP array. 
    $user_info  = json_decode($batchResponse[0]['body'], TRUE); 
    $feed   = json_decode($batchResponse[1]['body'], TRUE); 
    $friends_list  = json_decode($batchResponse[2]['body'], TRUE); 
    $photos   = json_decode($batchResponse[3]['body'], TRUE); 
//========= Batch requests over the Facebook Graph API using the PHP-SDK ends ===== 


} 

?> 
+0

如何共享您的代码以查看?不要忘记删除你的应用密钥,出于安全原因! – ifaour

+0

嘿,你发现哪个是问题? – luca

回答

1

最新版本我不完全知道为什么它的工作,但我将用户重定向到一个包含JavaScript SDK的页面(http://developers.facebook.com/docs/reference/javascript/),现在它正在运行!

0

用户没有授权的应用程序呢。试着像以前几个月我们工作的这 -

$user_id = $facebook->getUser(); 
echo $uid; 

if($user_id){ 
    try { 
    $user_profile = $facebook->api('/me'); 

    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user_id = null; 
    } 
}else{ 
    $login_url = $facebook->getLoginUrl(); 
    echo("<br>login url=".$login_url); 
}; 
+0

看起来像我目前使用的代码。它在一个名为“fbaccess.php”的文件中。 –

+0

抛出什么异常?您可以使用您获得的令牌在Graph Api Explorer中验证此功能吗? 图形Api资源管理器:https://developers.facebook.com/tools/explorer –

+0

除了这部分的一切似乎工作正常... $ user = $ facebook-> getUser(); 所以我很困惑。如果网站无法访问Facebook API,整个事情不应该停止工作吗? –

0

的FB应用正在经历你说的问题。 $ fb_user现在被返回undefined。 =(

+0

完全相同的脚本在另一个网站上为我工作,这使得它更令人困惑... –

0

更新SDK,听起来像你从几个星期打一个证书的问题了。

你可以找到https://github.com/facebook/facebook-php-sdk/

+0

我只是试图更新证书(以及facebook.php和base_facebook.php)。即使我登录,$ user变量仍然为0。 –

+0

@ViktorChangSvensson是否检查过错误日志 – phwd