How to Implement Login with LinkedIn in Website with PHP

Social Login is a popular term nowadays. A lot of websites allow the user to sign in using their social accounts. By using social login, a user doesn’t need to go through a verification process for your website. It also saves you from building a sign-up system. Social sites already verified users and websites can rely on the social sites for user verification.

There are several popular social login networks like Google, Facebook, Twitter, and LinkedIn used widely. In this article, we study login with LinkedIn using PHP. If you are interested in other social networks then look at our below articles.

Create a LinkedIn Application

For adding a LinkedIn login to your website, you first need to create the application on your LinkedIn account. We need the client id and client secret of the LinkedIn application. You also need to set the authorized redirect URL in the application.

  • Go to LinkedIn Developer Network.
  • Click on the ‘Create Application’ button.
  • Complete the basic information on the form.
  • Add YOUR_DOMAIN_URL/index.php in the redirect URLs field.
  • Copy the Client ID and Client Secret keys.

For the sake of the tutorial, I am using a local server and I set the redirect URL as http://localhost/linkedin/index.php. You can adjust this URL as per your requirement.

Next, click on the ‘Products’ tab. Here from the list of available products choose the ‘Sign In with LinkedIn’. Upon selecting this product, it will go for the review and then be included as an added product. This may take some time for review. In my case, it took around 10 minutes.

Installation of HybridAuth Library

HybridAuth is an open-source social sign-on PHP library. Using this library, you can add a social login for one or multiple providers like Google, Facebook, Twitter, LinkedIn, etc. This library makes the developer’s life easy. You don’t need to worry about handling the OAuth process. The HybridAuth library does it for us in their core. 

Head over to the terminal and install the HybridAuth package using the command:

composer require hybridauth/hybridauth

Next, create the config.php file and add your credentials, scope for LinkedIn OAuth, and the callback URL.

<?php
require_once 'vendor/autoload.php';

$config = [
    'callback' => 'YOUR_DOMAIN_URL/index.php',
    'keys'     => [
                    'id' => 'YOUR_CLIENT_ID',
                    'secret' => 'YOUR_CLIENT_SECRET'
                ],
    'scope'    => 'r_liteprofile r_emailaddress',
];

$adapter = new Hybridauth\Provider\LinkedIn( $config );

Make sure to replace the placeholders with the actual values. I passed the callback URL YOUR_DOMAIN_URL/index.php which you need to adjust.

Login with LinkedIn in PHP

We are all set with the configurations. Let’s go ahead and create 2 files index.php, logout.php in your project’s root directory. At first, add the code below in the index.php file.

<?php
require_once 'config.php';

try {
    $adapter->authenticate();
    $userProfile = $adapter->getUserProfile();
    print_r($userProfile);
    echo '<a href="logout.php">Logout</a>';
}
catch( Exception $e ){
    echo $e->getMessage() ;
}

In the above code, the HybridAuth library checks if a user is logged in with LinkedIn. If not then users will redirect to LinkedIn where they need to authorize their account with your application. On successful authorization, the user is redirected back to the index.php and using getUserProfile() method, we get the user’s information.

Here, you can store the user’s details in the database for later use.

Finally, to log out the user from the application you need to add the below code in a logout.php file.

<?php
require_once 'config.php';

try {
    if ($adapter->isConnected()) {
        $adapter->disconnect();
        echo 'Logged out the user';
        echo '<p><a href="index.php">Login</a></p>';
    }
}
catch( Exception $e ){
    echo $e->getMessage() ;
}

I hope you understand how to add a login with LinkedIn on a website with PHP. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

3 thoughts on “How to Implement Login with LinkedIn in Website with PHP

  1. How can I add a button to log in with LinkedIn? I’m using EspoCRM and I need to add a button on the login screen to give the user the option to log in with LinkedIn.

Leave a Reply

Your email address will not be published.