-2

这是API调用和通过Facebook登录的代码。问题是当它重定向到 Facebook 时,它没有登录。我的代码的哪一部分是错误的,我该如何解决?

require 'facebook/facebook.php';
require 'config/fbconfig.php';
require 'config/functions.php';
$url = $_REQUEST['lasturl'];

$facebook = new Facebook(array(
   'appId' => APP_ID,
          'secret' => APP_SECRET,
          'cookie' => true
    ));

$session = $facebook->getSession();

if (!empty($session)) {
    # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try {
    $uid = $facebook->getUser();
    $user = $facebook->api('/me');
} catch (Exception $e) {


}

    if (!empty($user)) {
        # User info ok? Let's print it (Here we will be adding the login and registering routines)
       // echo '<pre>';
        //print_r($user);
       // echo '</pre><br/>';
        $username = $user['name'];
        $user = new User();
        $userdata = $user->checkUser($uid, 'facebook', $username);
        if(!empty($userdata)){
            session_start();
            $_SESSION['id'] = $userdata['id'];
            $_SESSION['oauth_id'] = $uid;

            $_SESSION['username'] = $userdata['username'];
            $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
            header("Location: ".$url);
        }
    } else {
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
    }
} else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl();
    header("Location: " . $login_url);
}
4

2 回答 2

0

试试下面的代码:$session = $facebook->getSession();不工作

 $facebook = new Facebook(array(
       'appId' => APP_ID,
              'secret' => APP_SECRET,
              'cookie' => true
        ));

     $uid = $facebook->getUser();

    if ($uid) {
        # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
    try {

        $user = $facebook->api('/me');
    } catch (Exception $e) {


    }

        if ($uid) {
            # User info ok? Let's print it (Here we will be adding the login and registering routines)
           // echo '<pre>';
            //print_r($user);
           // echo '</pre><br/>';
            $username = $user['name'];
            $user = new User();
            $userdata = $user->checkUser($uid, 'facebook', $username);
            if(!empty($userdata)){
                session_start();
                $_SESSION['id'] = $userdata['id'];
                $_SESSION['oauth_id'] = $uid;

                $_SESSION['username'] = $userdata['username'];
                $_SESSION['oauth_provider'] = $userdata['oauth_provider'];
                header("Location: ".$url);
            }
        } else {
            # For testing purposes, if there was an error, let's kill the script
            die("There was an error.");
        }
    } else {
        # There's no active session, let's generate one
        $login_url = $facebook->getLoginUrl();
        header("Location: " . $login_url);
    }
于 2012-10-30T12:53:03.613 回答
0

实际上代码很好,获取登录凭据应该可以正常工作。但问题就在这里。

if (!empty($user)) 

我认为您应该在通过 API 调用获取用户信息之前将其放在首位。如果函数 getUser 返回一个 ID,那么您不需要获取当前会话来获取用户信息。

于 2012-10-30T12:42:44.427 回答