How to Implement Login with LinkedIn in Website with PHP

Social Login is a popular term nowadays. A lot of websites allows the user to sign in using their social accounts. Doing so, a user doesn’t need to go through a verification process for account creation on your website. It also saves you build a system of sign up flow. Social sites already did this and websites can rely on the social sites for user verification.

There are several popular social login networks in use like Google, Facebook, Twitter, LinkedIn, etc. In this article, we study login with LinkedIn using PHP. If you are interested in Google login then please check out our article How to Add Google OAuth Login in Website with PHP.

Create a LinkedIn Appliation

For adding LinkedIn login to your website, you first need to create the application on your LinkedIn account. We need client id, client secret of 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 so 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 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 developer’s life easy. As a developer, you don’t need to worry about handling the OAuth process. The HybridAuth library does it for us in their core. 

HybridAuth

Create a composer.json file in the project root folder and add the below code in it.

{
    "require" : {
        "hybridauth/hybridauth" : "~3.0"
    }
}

Next, open the terminal in your project root directory and run the command:

composer install

Create the config.php file and add your credentials, scope for LinkedIn OAuth login, 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 as YOUR_DOMAIN_URL/index.php which you need to adjust as per your requirement.

Login with LinkedIn in PHP

We all set with the configurations. So 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, HybridAuth library checks if a user is logged in with LinkedIn. If it is not then the user will redirect to LinkedIn where they need to authorize their account with your application. On successful authorization, user redirect back to the index.php and using getUserProfile() method it prints the user information.

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 login with LinkedIn on a website with PHP. Please share your thoughts 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. Required fields are marked *