1

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

但是, $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 =====


}

?>
4

4 回答 4

1

我不完全确定它为什么会起作用,但我将用户重定向到包含 Javascript SDK 的页面 (http://developers.facebook.com/docs/reference/javascript/),现在它可以正常工作了!

于 2012-12-19T17:14:53.663 回答
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);
};
于 2012-12-18T09:44:09.090 回答
0

我们之前工作了几个月的 FB 应用程序现在遇到了您所说的问题。$fb_user 现在返回未定义。=(

于 2012-12-18T23:53:36.940 回答
0

更新您的 SDK,听起来您在几周前遇到了证书问题。

您可以在https://github.com/facebook/facebook-php-sdk/找到最新版本

于 2012-12-19T15:33:27.700 回答