将Codeigniter框架与Facebook的PHP SDK和Javascript SDK结合使用。 这是我的代码。
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->config->load('facebook');
$this->load->library('Facebook',array('appId'=>$this->config->item('appID'),'secret'=>$this->config->item('appSecret')));
}
function fblogin(){
$base_url=$this->config->item('base_url');
$user = $this->facebook->getUser();
log_message('info','>>>>>>>>>>>>User id is::'.$this->config->item('appID').'::'.$this->config->item('appSecret').' For session '.session_id());
if($user == 0){
$loginUrl = $this->facebook->getLoginUrl(array("scope"=>"user_about_me"));
redirect("$loginUrl","location");
}
if($user){
try{
$user_profile = $facebook->api('/me');
$params = array('next' => $base_url.'fbci/logout');
$ses_user=array('User'=>$user_profile,
'logout' =>$facebook->getLogoutUrl($params)
);
$this->session->set_userdata($ses_user);
redirect('/','refresh');
header('Location: '.$base_url);
}catch(FacebookApiException $e){
log_message('error',error_log($e));
$user = NULL;
}
}
的Javascript
FB.login(function(response) {
console.log('fb respone'+response.serialize());
if(response.authResponse) {
parent.location ="<?php echo site_url('fbci/fblogin');?>";
}
},{scope: 'email,read_stream,publish_stream,user_birthday,user_location,user_work_history,user_hometown,user_photos'});
});
在libraries文件夹中添加了Facebook.php
和base_facebook.php
。这段代码看起来很直接,但没有弄错。