[英]facebook sdk getuser() always return 0 after login?
Below is my controller. 以下是我的控制器。 I am trying to login using facebook php sdk.
我正在尝试使用Facebook php sdk登录。 In it
getuser()
function always return 0 after login. 在其中,
getuser()
函数始终在登录后返回0。 What is the problem? 问题是什么?
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User_Authentication extends CI_Controller
{
function __construct() {
parent::__construct();
// Load user model
$this->load->model('user');
}
public function index(){
// Include the facebook api php libraries
include_once APPPATH."libraries/facebook-api-php-codexworld/facebook.php";
// Facebook API Configuration
$appId = '1863771173889202';
$appSecret = 'e86b25ccab12bc88c46aac322ebc40eb';
$redirectUrl = base_url() . 'user_authentication/';
$fbPermissions = 'email';
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
$fbuser = $facebook->getUser();
if ($fbuser) {
$userProfile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
// Preparing data for database insertion
$userData['oauth_provider'] = 'facebook';
$userData['oauth_uid'] = $userProfile['id'];
$userData['first_name'] = $userProfile['first_name'];
$userData['last_name'] = $userProfile['last_name'];
$userData['email'] = $userProfile['email'];
$userData['gender'] = $userProfile['gender'];
$userData['locale'] = $userProfile['locale'];
$userData['profile_url'] = 'https://www.facebook.com/'.$userProfile['id'];
$userData['picture_url'] = $userProfile['picture']['data']['url'];
// Insert or update user data
$userID = $this->user->checkUser($userData);
if(!empty($userID)){
$data['userData'] = $userData;
$this->session->set_userdata('userData',$userData);
} else {
$data['userData'] = array();
}
} else {
$fbuser = '';
$data['authUrl'] = $facebook->getLoginUrl(array('redirect_uri'=>$redirectUrl,'scope'=>$fbPermissions));
}
$this->load->view('user_authentication/index',$data);
}
public function logout() {
$this->session->unset_userdata('userData');
$this->session->sess_destroy();
redirect('/user_authentication');
}
}
I am a beginner here so please help me resolve my issue. 我是这里的初学者,所以请帮助我解决问题。
I think you need to check the redirect_uri and put the proper URL in it. 我认为您需要检查redirect_uri并将正确的URL放入其中。 Here is the line of code I thinks its help you:
这是我认为可以帮助您的代码行:
$loginUrl = $facebook->getLoginUrl(array (
'display' => 'popup',
'redirect_uri' => 'http://localhost/demo/index.php'
));
In window.fbAsyncInit I have set cookie and oauth to true and this worked! 在window.fbAsyncInit中,我已将cookie和oauth设置为true,这可行!
cookie:true, 饼干:真实,
oauth : true. oauth:是的。
Make sure that In your php you also have 确保在您的php中也有
cookie' => true. cookie'=> true。
That will enable the php code to integrare with the JS. 这将使php代码与JS集成。
And you need to make sure the user has logged in with: 并且您需要确保用户使用以下方式登录:
$loginUrl = $facebook->getLoginUrl(array( 'scope' => 'read_stream,etc..')); $ loginUrl = $ facebook-> getLoginUrl(array('scope'=>'read_stream,..'));
The scope is defined by the permissions stated here 范围由此处所述的权限定义
After that you should be able to do: 之后,您应该可以执行以下操作:
$user = $facebook->api("/me"); $ user = $ facebook-> api(“ / me”);
Which will get an array associated with the user, $user['id'] is probably the one you'll want to store as well as any other information like email address - be sure to request for email in the scope 它将获得与用户关联的数组,$ user ['id']可能是您要存储的数组,以及其他任何信息(如电子邮件地址)-请确保在范围内请求电子邮件
Hope that solves the problem! 希望能解决问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.