Skip to main content

Command Palette

Search for a command to run...

Facebook PHP SDK - Authentication and Get User Access Token

Facebook API and PHP

Updated
3 min read
Facebook PHP SDK - Authentication and Get User Access Token

Integrating Facebook Login into your web application simplifies user authentication and enables seamless access to user information. The Facebook PHP SDK provides a straightforward way to authenticate users with Facebook and retrieve their data. This guide will walk you through the steps to integrate Facebook Login using the Facebook PHP SDK in your project.

Why Use Facebook Login?

Integrating Facebook Login improves user experience by allowing quick authentication without requiring users to remember additional credentials. It also provides access to rich user data for enhanced personalization and functionality.

Step 1: Set up a Facebook App

Before using the Facebook PHP SDK, you need to create a Facebook app:

  1. Go to the Facebook Developers.

  2. Log in or register as a Facebook developer.

  3. Create a new app and follow the setup instructions in the Facebook Developer Dashboard.

  4. Note your App ID and App Secret, as you’ll need them in the next steps.

Step 2: Install the Facebook PHP SDK

Once you have your Facebook app set up, you'll need to install the Facebook PHP SDK in your project. You can do this using Composer, a package manager for PHP.

To install the Facebook PHP SDK using Composer, run the following command in your project directory:

composer require sohaibilyas/facebook-php-sdk

Step 3: Create an Instance of the Facebook Class

After you've installed the Facebook PHP SDK, you can create an instance of the Facebook class in your PHP code. To do this, you'll need to provide your app ID, app secret, and redirect URL. You can get these values from your Facebook app dashboard.

use SohaibIlyas\FacebookPhpSdk\Facebook;

$facebook = new Facebook([
    'app_id' => 'your-app-id-here',
    'app_secret' => 'your-app-secret-here',
    'redirect_url' => 'https://your-redirect-url-here'
]);

Step 4: Handle the Facebook Redirect

After a user logs in with Facebook, Facebook will redirect them back to your website. You'll need to handle this redirect in your PHP code to get the user's access token. The handleRedirect() of the Facebook class can help you do this.

$facebook->handleRedirect(function($user) {
    // Save the user access token in the database for later use
    $_SESSION['access_token'] = $user->access_token;
});

Step 5: Check if Access Token is Set

Once you've saved the access token, you can check if it's set in the session or database. If it is, you can set the access token for all requests using the setAccessToken() method. You can also set the default response type for all requests using the setResponseType() method.

if (isset($_SESSION['access_token'])) {
    // Set default user access token
    $facebook->setAccessToken($_SESSION['access_token']);

    // Default response type e.g. object, json, array
    $facebook->setResponseType('json');

    // Getting Facebook user information
    print_r($facebook->getUser());
} else {
    // If access token not set, show login with Facebook URL
    echo $facebook->getLoginUrl(['email', 'public_profile']);
}

Step 6: Access User Information

If the access token is set, you can access the user's information using the getUser() method. This method returns an object containing the user's Facebook ID, email, name, and other information, depending on the permissions you requested.

print_r($facebook->getUser());

Integrating with Facebook can be a great way to streamline authentication and user information in your web application.

Facebook PHP SDK

Part 1 of 1

More from this blog

Sohaib Ilyas

15 posts

Web Developer / Laravel / Vuejs / Social Media APIs