0

我正在尝试使用 codeigniter 和 FB PHP SDK 登录 FB。

但它一直给我与facebook链接相同的登录信息,如果用户登录它并没有捕捉到,我是否错过了诸如会话之类的东西?

既然我们在这里,我如何才能获得扩展会话以及在用户离线时我应该保存哪些数据以发布到页面?

这是我的控制器:

function __construct () {
    parent::__construct();      
    $this->load->helper('url_helper');
    $this->load->add_package_path(APPPATH.'third_party/facebook/');
    $this->config->load('facebook', TRUE);
    $this->load->library('facebook', array( 'appId'  => $this->config->item('appId', 'facebook'), 'secret' => $this->config->item('secret', 'facebook') ) );
}

public function index() {
    $data['LoginURL']   = $this->facebook->getLoginUrl( array('scope' => 'manage_pages') );
    $data['user']       = $this->facebook->getUser();
    if ($data['user']) {
      try {
        $data['user_profile']   = $this->facebook->api('/me');
        $data['AccessToken']    = $this->facebook->getAccessToken();
      } catch (FacebookApiException $e) {
        // The access token we have is not valid
        $data['user'] = null;
      }
    }
    $this->load->view('fb_welcome', $data);
}

这是视图:

<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ( $user ): ?>      
    User <em><?php echo $user ?></em> is Logged in, AccessToken is <?php echo $AccessToken; ?>
    <?php print_r($user_profile); ?>
<?php else: ?>
    <?php echo anchor($LoginURL, "Login with facebook"); ?>
<?php endif; ?>

</body>
</html>
4

1 回答 1

0

我不知道代码点火器,但希望这应该证明是一个很好的起点,只需在包含中链接到您的 fb sdk,用您自己的替换变量,当用户未登录时,它应该会给用户一个弹出窗口:

// include the FB SDK
require_once(APPLICATION_PATH . '/../library/facebook/facebook.php');

// create the application instance
$facebook = new Facebook(array(
    'appId'  => $this->facebookConfig->appid,
    'secret' => $this->facebookConfig->appsecret
));

// get the user id
$user = $facebook->getUser();
if(!empty($user)) {
    // We have a user ID, so probably a logged in user.
    // If not, we'll get an exception, which we handle below.
    try {

        // do something

    } catch(FacebookApiException $e) {
        // If the user is logged out, you can have a 
        // user ID even though the access token is invalid.
        // In this case, we'll get an exception, so we'll
        // just ask the user to login again here.
        $params = array(
            'scope'         => $this->permissions,
            'redirect_uri'  => $this->facebookConfig->applink
        );
        $loginUrl = $facebook->getLoginUrl($params);

        // log errors
        error_log($e->getType());
        error_log($e->getMessage());

        // redirect if not logged in or not enough permissions
        //echo "<script>top.location=\"".$loginUrl."\";</script>";die;
    }

    // Give the user a logout link 
    //echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';
} else {

    $params = array(
            'scope'         => $this->permissions,
            'redirect_uri'  => $this->facebookConfig->applink
        );
    $loginUrl = $facebook->getLoginUrl($params);

    // log errors
    error_log($e->getType());
    error_log($e->getMessage());

    // redirect if not logged in or not enough permissions
    echo "<script>top.location=\"".$loginUrl."\";</script>";die;

}
于 2012-09-12T11:00:22.113 回答