0
<?php

require 'facebook/facebook.php';
require 'config/fbconfig.php';
require 'config/functions.php';

define('APP_ID', '******************');
define('APP_SECRET', '***********************');

$facebook = new Facebook(array(
            'appId' => '******************',
            '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();
        print_r($uid);
        $user = $facebook->api('/me');
        facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; 
        facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
    } catch (Exception $e) {




    }

    if ($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: home.php");
        }
    } 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();
     $loginUrl = $facebook->getLoginUrl( array(
                           'canvas'    => 1,
                           'fbconnect' => 0,
                           'req_perms' => 'email,publish_stream',
                       'next' => 'http://localhost/login_twitbook/',
                              'cancel_url' => 'http://localhost/login_twitbook/'
                      ) );
    header("Location: " . $login_url);
}
?>

$user = $facebook->api('/me'); 不能正常工作它总是什么都不返回。请帮我解决这个问题。我正在处理 localhost.$uid = $facebook->getUser() 它返回值但 $user 不返回

4

1 回答 1

0

尝试更改 base_facebook.php 文件中的 getCode 函数,将所有 $_REQUEST 更改为 $_GET。不知道为什么这个问题还没有解决,但我一直在努力弄清楚为什么在我这样做之前它会为用户返回 0,现在它可以完美地工作了。

应该在文件的第 680 行或附近,并且有 4 个要更改。

  protected function getCode() {
if (isset($_GET['code'])) {
  if ($this->state !== null &&
      isset($_GET['state']) &&
      $this->state === $_GET['state']) {

    // CSRF state has done its job, so clear it
    $this->state = null;
    $this->clearPersistentData('state');
    return $_GET['code'];
  } else {
    self::errorLog('CSRF state token does not match one provided.');
    return false;
  }
}

return false;

}

于 2012-10-18T11:47:24.447 回答