<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller Extends CI_Controller
{
var $data;
var $master;
function __construct()
{
parent::__construct();
$this->data = array();
$this->master = "layout/master";
parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );
$CI = & get_instance();
$CI->config->load("facebook",TRUE);
$config = $CI->config->item('facebook');
$this->load->library('Facebook', $config);
$user = $this->facebook->getUser();
if ($user == 0)
$this->data['login_url'] = $this->facebook->getLoginUrl(array("scope" => "user_about_me"));
else{
$user = $this->facebook->api('/me');
$this->data['logout_url'] = $this->facebook->getLogoutUrl();
}
print_r($user);
print_r($this->facebook->getAccessToken());
}
}
为什么我成功获取AccessToken,但是$ user仍然返回0? 我搜索了遍布stackoverflow和google。
如果我不清楚的话,facebook会成功将我重定向回我的网站。答案 0 :(得分:1)
调整代码
function __construct()
{
parent::__construct();
$this->data = array();
$this->master = "layout/master";
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 facebook_login()
{
$user = $this->facebook->getUser();
if($user == 0){
$loginUrl = $this->facebook->getLoginUrl(array("scope"=>"user_about_me"));
redirect("$loginUrl","location");
} else {
$user_info = $this->facebook->api('/me');
print_r($user_info);
}
}
答案 1 :(得分:0)
我花了几个小时尝试了很多修复,发现我的域名在Facebook中设置为http://domain.co.uk
,而getLoginUrl
则指向http://www.domain.co.uk
。