skip to Main Content

Below is my controller. I am trying to login using facebook php sdk. In it getuser() function always return 0 after login. 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.

2

Answers


  1. I think you need to check the redirect_uri and put the proper URL in it.
    Here is the line of code I thinks its help you:

    $loginUrl = $facebook->getLoginUrl(array ( 
    'display' => 'popup',
    'redirect_uri' => 'http://localhost/demo/index.php'
    ));
    
    Login or Signup to reply.
  2. In window.fbAsyncInit I have set cookie and oauth to true and this worked!

    cookie:true,
    oauth : true.

    Make sure that In your php you also have

    cookie’ => true.

    That will enable the php code to integrare with the JS.

    And you need to make sure the user has logged in with:

    $loginUrl = $facebook->getLoginUrl(array( ‘scope’ => ‘read_stream,etc..’));

    The scope is defined by the permissions stated here

    After that you should be able to do:

    $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

    Hope that solves the problem!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search