[英]If condition is always returning zero while getting the user in " $this->'library_name'->getUser()
我正在嘗試此代碼:
$this->load->library('fbconnect');
if ($this->fbconnect->user){
echo "<pre>";
print_r($this->fbconnect->user);
echo "</pre>";
}else{
echo "could not login at this tym";
}
它始終顯示“ else”狀態的回聲部分。 “ fbconnect”是包含facebookconnect代碼的庫名稱,“ user”從fb函數“ getUser()”獲取值
它是:“ fbconnect.php”庫的代碼:
class Fbconnect extends Facebook{
public $user = NULL;
public $user_id = NULL;
public function Fbconnect(){
$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);
}
}
}
}
登錄功能可以放在控制器本身中。 將Facebook.php(facebook php sdk文件)和base_facebook.php(facebook php sdk文件)添加到application/libraries
class Fbconnect extends CI_Controller{
public function Fbconnect()
{
parent::__construct();
$CI = & get_instance();
$CI->config->load("facebook",TRUE);
$config = $CI->config->item('facebook');
$this->load->library('Facebook', $config);
}
function facebook_login()
{
$me = $this->facebook->getUser();
if($me == 0){
$loginUrl = $this->facebook->getLoginUrl();
//Do the necessary with $loginUrl
}else{
try{
$user = $this->facebook->api('/me');
return $user; //or echo $user
}catch(FacebookApiException $e)
{
error_log($e);
}
}
}
}
現在調用該函數,即http://yoursite.com/index.php/Fbconnect/facebook_login
檢查日志文件或使用
if($this->user_id){
try{
$me = $this->api('/me');
$this->user = $me;
}
catch(FacebookApiException $e){
echo $e->getMessage();
}
}
查看錯誤消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.