[英]If condition is always returning zero while getting the user in " $this->'library_name'->getUser()
I am trying this code : 我正在尝试此代码:
$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";
}
It is always displaying the echo part of 'else' condition. 它始终显示“ else”状态的回声部分。 'fbconnect' is the library name containing facebookconnect code and 'user' is getting the value from fb function 'getUser()' “ fbconnect”是包含facebookconnect代码的库名称,“ user”从fb函数“ getUser()”获取值
Here it is: the code of 'fbconnect.php' library: 它是:“ 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);
}
}
}
}
The login function can be put in controller itself. 登录功能可以放在控制器本身中。 Add Facebook.php(facebook php sdk file) and base_facebook.php(facebook php sdk file) to application/libraries
将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);
}
}
}
}
Now call the function ie http://yoursite.com/index.php/Fbconnect/facebook_login
现在调用该函数,即http://yoursite.com/index.php/Fbconnect/facebook_login
check log file or use 检查日志文件或使用
if($this->user_id){
try{
$me = $this->api('/me');
$this->user = $me;
}
catch(FacebookApiException $e){
echo $e->getMessage();
}
}
to see error message. 查看错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.