Login with LinkedIn using PHP and LinkedIn oAuth API Live Demoa and Download

Abhigyan Singh 29th Sep 2020

LinkedIn is the world’s largest professional network with hundreds of millions of members and growing rapidly. It connects the world’s professionals to make them more productive and successful. You can add a login with the LinkedIn facility on your website to simplify the registration process.

Here we will let you know how you can add login with the LinkedIn button on your website or web application. You would not require to filling all details from the user into the registration form.

By clicking on the button you will redirect to the LinkedIn site will enter user credentials and LinkedIn verifies from the database.

After verifying from the database it will redirect to your website if your credentials were correct otherwise it will show an error that email or password is wrong. Here these are the following steps to do this as follows:

1. Go to https://www.linkedin.com/developer/apps.  Click on Create Application to create a new application and fill all required details.

2. After filling all details click on submit details as shown below. Enter the app name what you want in the name field. In my case the app name is Discussdesk.

3. After Submitting all details will have the following screen with Client and secret id shown below. Here you have to put your website redirect URL or wherever you want to redirect after login.

4. Create a config.php file with the following code as shown below and put your client, secret key, and redirect the URL of your created linked app.

<?php
$baseURL = 'YOUR_URL';
$callbackURL = 'CALLBACK_URL';
$linkedinApiKey = 'Client id';
$linkedinApiSecret = 'Client Secret';
$linkedinScope = 'r_basicprofiler_emailaddress';
?>

5. Create another file index.php with the following code as shown below. In this file, we put a simple image of the LinkedIn login button in else part. If the condition or part contains library files of authentication and functions to get user data. The code of if part will be executed after redirection or authentication from LinkedIn.

<?php
session_start();
error_reporting(0);
if(($_GET['login']=='linkedin') && ($_SESSION['login']=='1'))
{

include_once("config.php");
include_once("library/http.php");
include_once("library/oauth_client.php");

$client = new oauth_client_class;

$client->debug = false;
$client->debug_http = true;
$client->redirect_uri = $callbackURL;

$client->client_id = $linkedinApiKey;
$application_line = __LINE__;
$client->client_secret = $linkedinApiSecret;
$_SESSION['requestToken'] = serialize($client->client_secret);

/* API permissions
*/
$client->scope = $linkedinScope;
if (($success = $client->Initialize())) {
if (($success = $client->Process())) {
if (strlen($client->authorization_error)) {
$client->error = $client->authorization_error;
$success = false;
} elseif (strlen($client->access_token)) {
$success = $client->CallAPI(
'https://api.linkedin.com/v1/people/~:(id,email-address,first-name,last-name,location,picture-url,public-profile-url,formatted-name)',
'GET', array(
'format'=>'json'
), array('FailOnAccessError'=>true), $user);
}
}
$success = $client->Finalize($success);
}
if ($client->exit) exit;
if ($success) {

$result = '<h1>LinkedIn Profile Details </h1>';
$result .= '<imgsrc="'.$user->pictureUrl.'">';
$result .= '<br/>LinkedIn ID : ' . $user->id;
$result .= '<br/>Email : ' . $user->emailAddress;
$result .= '<br/>Name : ' . $user->firstName.' '.$user->lastName;
$result .= '<br/>Location : ' . $user->location->name;
$result .= '<br/>Logout from <a href="logout.php">LinkedIn</a>';
echo '<div>'.$result.'</div>';

} else {
$_SESSION["err_msg"] = $client->error;
}
}else{

echo '<a href="index.php?login=linkedin"><imgsrc="linkedin.png" /></a>';
$_SESSION['login'] = '1';

}
?>

6. Create another file logout.php to logout from LinkedIn or destroy session variable.

<?php
session_start();
session_destroy();
header("location:index.php");
?>

Now run attached download LinkedIn folder will have following screen as shown below. Login from LinkedIn account will get all basic user data.

Now we have learnt about Login with Linkedin using PHP. You can download and use this code. If you have still any issue to this code feel free to ask in comment section

Authored By Abhigyan Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk