[英]Facebook login api + codeigniter
Facebook的配置
<? if ( ! defined('BASEPATH')) exit('No direct script access allowed');
<?php
$config['appId'] = APP_ID;
$config['secret'] = APP_SECRET;
?>
控制器代碼:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Facebooklogin extends CI_Controller
{
public function Main()
{
parent::__construct();
parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );
$CI = & get_instance();
$CI->config->load("facebook",TRUE);
$config = $CI->config->item('facebook');
$this->load->library('Facebook', $config);
}
function index()
{
// Try to get the user's id on Facebook
$userId = $this->facebook->getUser();
// If user is not yet authenticated, the id will be zero
if($userId == 0)
{
// Generate a login url
$data['url'] = $this->facebook->getLoginUrl(array('scope'=>'email'));
$this->load->view('signin', $data);
}
else
{
// Get user's data and print it
$user = $this->facebook->api('/me');
print_r($user);
}
}
}
?>
查看文件
<a href="<?php echo $url;?>">
<button class="btn btn-mini btn-facebook">
<i class="icon-facebook"></i>
| Connect with Facebook
</button>
</a>
我已經集成了此登錄api代碼。 嘗試登錄時,URL不會重定向到Facebook登錄頁面。 我不知道如何重定向url.Also我已經添加了配置文件。
將您的代碼更改為類似以下內容:
...
$login_url = $this->facebook->getLoginUrl(array('scope'=>'email'));
header( 'Location: ' . $login_url );
die();
...
基本上,如果沒有登錄,則將標頭直接重定向到登錄頁面。或者,您可以在signin
視圖中執行JavaScript重定向:
<script type="text/javascript">
window.location = '<?php echo $url; ?>';
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.