我正在尝试在 codeigniter 应用程序上使用 facebook 登录。我有一个登录按钮,可以让他们授予权限,然后将他们重定向到应该在数组中显示他们的 facebook 信息的页面,但是 getUser 一直返回 0,即使在我登录后也是如此。
我不知道从哪里开始寻找错误,因为这是我第一次潜入 facebook sdk
这是我的代码:
php
Facebook 连接库:
<?php include(APPPATH.'libraries/facebook/facebook.php');
class Fbconnect extends Facebook{
public $user = null;
public $user_id = null;
public $fb = false;
public $fbSession = false;
public $appkey = 0;
public function __construct() {
$ci =& get_instance();
$ci->config->load('facebook', TRUE);
$config = $ci->config->item('facebook');
parent::__construct($config);
$this->user_id = $this->getUser();
$me = null;
if ($this->user_id) {
try{
$me = $this->api('/me');
$this->user = $me;
} catch(FacebookApiException $e) {
error_log($e);
}
} else {
}
}
}
?>
我的主要功能
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->login();
}
public function login() {
$this->load->library('fbconnect');
$this->load->view('login');
}
public function facebook_request() {
$this->load->library('fbconnect');
$data = array(
'redirect_uri' => 'http://localhost/ci_test/main/handle_facebook_login',
'scope' => 'email'
);
redirect($this->fbconnect->getLoginUrl($data));
}
public function handle_facebook_login() {
$this->load->library('fbconnect');
if ($this->fbconnect->user) {
print_r($this->fbconnect->user);
} else
{
echo "Couldn't log in";
echo "User Id";
echo $this->fbconnect->user_id;
}
}
}
登录链接
<h1>Login!</h1>
<a href='main/facebook_request'> Connect with facebook </a>