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 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 review and then be included as an added product. This may take some time to 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 at its 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, the 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. 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.

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

  1. Here I also tried your code, but itss not working. it gives me the following error
    Signed API request has returned an error. HTTP error 401. Raw Provider API response: {“serviceErrorCode”:65606,”message”:”Application is disabled”,”status”:401}. got the same error like Dalal Ziada

    I have got permissions of email, w_member_social, profile, openid

  2. I try your code, but unfortunately it is not working. it gives me the following error
    Signed API request has returned an error. HTTP error 401. Raw Provider API response: {“serviceErrorCode”:65606,”message”:”Application is disabled”,”status”:401}.

  3. 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. Required fields are marked *