Facebook Provider for OAuth 2.0 Client

Facebook Provider for OAuth 2.0 Client supported following version

  • PHP 7.3

  • PHP 7.4

  • PHP 8.0 and above

If your Php version is below above version, then you could consider to use Facebook SDK for PHP (v5) which is released by META official.

However, Facebook SDK for PHP (v5) had not update since 2020, and it is no more support PHP version 8 and above. But it stil best selection if you are using Php version 5.

Installation

Composer

You can use Composer or simply Download the Release. Follow the installation instructions if you do not already have composer installed.

Once composer is installed, execute the following command in your project root to install this library:

composer require league/oauth2-client
composer require league/oauth2-facebook

Finally, be sure to include the autoloader at your php coding :

require_once 'vendor/autoload.php';

Facebook Developer Setup

Open Facebook Developer, Select the [Create App] on the right upper corner and create a new app.

Select [Other] then select [Consumer]

Add an [App name]

Find the Facebook Login at the third card and click the [Set Up] button

Select [Web]

Add the site URL, click [Save] and keep click [Continue] or [Save] until [Step 5 Next Steps]

Go to [Setting] under the [Facebook Login] from your left panel, and add your [Valid Oauth Redirect URLs]. [Save Changes] after done

After then, select [Basic] under [App settings] at left side banner and fill in the [App domain], [Privacy Policy URL] and [Contact Email]

Copy down the [App ID] and [App secret]. It will be useful at code later on.

Also, scrolling down to below and click [+ Add platform], select [Website] and fill in the [Site URL] which is same with your [Valid Oauth Redirect URLs]

Remember to made your App Mode to [Live] mode

Implementation on Your Website

login_fb.php

replace the clientID and clientSecret to your [client ID] and [Client Secret]

<?php
session_start();
require_once 'vendor/autoload.php';

// Facebook Login Start  -------------------------------------------------------------------------------------------------------------------

// Initialize Facebook API
// Dont touch unless ID/API expired
$provider = new \League\OAuth2\Client\Provider\Facebook([
    'clientId'          => 'xxxxxxxxxxxxxxx',
    'clientSecret'      => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    'redirectUri'       => 'http://localhost:3000/login_fb.php',
    'graphApiVersion'   => 'v2.10',
]);


if (!isset($_GET['code'])) {
    // Step 1: Redirect the user to Facebook to authorize your app
    $auth_url = $provider->getAuthorizationUrl(['scope' => ['email']]);
    $_SESSION['oauth2state'] = $provider->getState();
    header("Location: $auth_url");
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
    unset($_SESSION['oauth2state']);
    echo 'Invalid state.';
} else

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);

$user = $provider->getResourceOwner($token);
$link = mysqli_connect('ftp.chinaetravel.com', 'mapmama', 'Ipoh23a@', 'mapmama');
$id = $user->getId();
$name = $user->getName();
$firstName = $user->getFirstName();
$lastName = $user->getLastName();
$email = $user->getEmail();
$hometown = $user->getHometown();
$pictureUrl = $user->getPictureUrl();
$isDefaultPicture = $user->isDefaultPicture();
$gender = $user->getGender();
$getlink = $user->getLink();
$maxAge = $user->getMaxAge();
$minAge = $user->getMinAge();
$password =  password_hash($id, PASSWORD_DEFAULT); // take google id as password(after encrypted)
$username = "$firstName, $lastName"; //the reason use format [$first name, $last name] instead of $name, is to avoid repeated username in users table

echo $username;

// Facebook Login End    -------------------------------------------------------------------------------------------------------------------



button.php

Create a login button to header to login_fb.php

PHP

    // if fb login
    if (isset($_POST['fbLogin'])) {
        header("location: login_fb.php");
    }

Html

<button name="fbLogin" class="login-with-facebook-btn">
  Sign in with Facebook
</button>

Last updated