Facebook使用PHP SDK登录

时间:2012-09-12 10:51:33

标签: php facebook codeigniter facebook-php-sdk

我正在尝试使用codeigniter& FB PHP SDK。

但它一直给我与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>

1 个答案:

答案 0 :(得分:0)

我不知道代码点火器,但希望这应该是一个很好的起点,只需链接到包含的fb sdk,用你自己的vars替换vars,它应该在用户没有登录时给用户弹出:

// 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;

}