[英]Facebook login using PHP SDK
我正在嘗試使用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>
我不知道代碼點火器,但是希望這可以證明是一個很好的起點,只需在include中鏈接到您的fb sdk,用您自己的var替換var,當用戶未登錄時,它應該會給用戶一個彈出窗口:
// 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.