Integrate Facebook login is the most used feature for today’s web application. Login with Facebook feature helps users to log into the web application easily. Which means that your web application receives more users We’ve already published Login with facebook using .PHP CodeIgniter 3.1.11 that will help you to implement Facebook login in PHP CodeIgniter 3.1.11 . In this tutorial, we’re going to explain how to integrate Facebook login in CodeIgniter using Facebook PHP SDK with Facebook Graph API.
Before you begin to implement Facebook login in CodeIgniter 3.1.11 using SDK v5, create a Facebook app in Facebook developers panel and get the App ID and App Secret. You should need to set Site URL that would be the same of user authentication controller URL (https://example.com/login). Also need to specify the Redirect URL, App ID, and App Secret in your script at the time of connecting with Facebook Graph API.
Getting started with the Facebook SDK for PHP
Whether you're developing a website with Facebook login, creating a Facebook Canvas app or Page tab, the Facebook SDK for PHP does all the heavy lifting for you making it as easy as possible to deeply integrate into the Facebook platform.
Autoloading & namespaces
The Facebook SDK for PHP v5 is coded in compliance with PSR-4. This means it relies heavily on namespaces so that class files can be loaded for you automatically. It would be advantageous to familiarize yourself with the concepts of namespacing and autoloading if you are not already acquainted with them.
System requirements
- PHP 7.4 or greater
- Composer (optional)
Installing the Facebook SDK for PHP
There are two methods to install the Facebook SDK for PHP.
- The recommended installation method is by using Composer. If are unable to use Composer for your project,
- You can still install the SDK manually by downloading the source files and including the autoloader.
I recommended Installing with Composer
Composer is the recommended way to install the Facebook SDK for PHP. Simply run the following in the root of your project. change directory in codeigniter library and install sdk
composer require facebook/graph-sdk
Authentication and authorisation
The SDK can be used to support logging a Facebook user into your site using Facebook Login which is based on OAuth 2.0.Most all request made to the Graph API require an access token. We can obtain user access tokens with the SDK using the helper classes.
Configuration and setup
Configuration requirements
- app_id GET APP ID
- app_secret
- call_back_url
Create a controller Or class application/Welcome.php in codeigniter and configuration facebook SDK
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . '/libraries/vendor/autoload.php';
Before we can send requests to the Graph API, we need to load our app configuration into the Facebook\Facebook service.which we are doing inside index method
public function index()
{
$fb = new Facebook\Facebook([
'app_id' => '339896957191766',
'app_secret' => 'fa7691669f8e031d7fa90dce4c130da2',
'default_graph_version' => 'v2.10',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$this->data['loginurl'] = $helper->getLoginUrl('https://stackoverlode.com/login_callback.php',$permissions);
$this->load->view('login-facebook',$this->data);
}
Like in the given example we are creating an object of Facebook class and using its property and loading view page while creating a button login with facebook
$this->load->view('login-facebook',$this->data);
Create a view page view/login-facebook.php
<a href="<?= $loginurl ?>">Login With facebook </a>