How to login with LinkedIn oAuth2 in PHP

In this tutorial today we will show you how to login with LinkedIn using PHP with very simple and easy code. If you familiar with php then you can easily integrate code of login with LinkedIn.

Here i am showing you step by step in easy way how to create very simple login with LinkedIn using basic php code.

Step 1.Create a LinkedIn Application and get all the details

First of we have to create a new LinkedIn application open this link https://www.linkedin.com/secure/developer and click on button create app. Fill all the required details and click on create app button after submit the form you get your API key and Secret Key from linkedin.

Login With LinkedIn Using PHP v2
Login With LinkedIn in PHP v2

Step 2. Update config details

Update your API key and Secret Key which you got from LinkedIn follow by step 1. Create a php file copy and past below code and enter details like callback url, key, secret key etc and save it.

$config['callback_url'] = 'Your_Callback_URL';
$config['Client_ID'] = 'Your_Client_ID';
$config['Client_Secret'] = 'Your_Client_Secret_Key';

Step 3. Create Login button

In this step simply we create a login button once user click on this button redirect on LinkedIn for check whether user login with LinkedIn or not.

<a href="<?php echo 'https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=815lmmgys5jhrn&redirect_uri='.$config['callback_url'].'&state=98765EeFWf45A53sdfKef4233&scope=r_liteprofile%20r_emailaddress';?>" class="btn-get-started scrollto">Login with linkedin</a>

Step 4. Get Access Token and user details

Here i am writing full code of login with LinkedIn once you click on login button it will redirect on LinkedIn and ask for login and allow information with website once user allow then redirect on website and get Access Tokes using access token get the detail of user.

if ($config['Client_ID'] === '' || $config['Client_Secret'] === '') {
	echo 'You need a API Key and Secret Key to test the sample code. Get one from <a href="https://www.linkedin.com/developer/apps/">https://www.linkedin.com/developer/apps/</a>';
	exit;
}

function curl($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($ch);
	curl_close($ch);
	return $result;
}

if(isset($_GET['code']))
{
	// Get access token
	$url = 'https://www.linkedin.com/oauth/v2/accessToken?';
	$param = 'grant_type=authorization_code&code='.$_GET['code'].'&redirect_uri='.$config['callback_url'].'&client_id='.$config['Client_ID'].'&client_secret='.$config['Client_Secret'];
	$return = (json_decode(curl($url.$param),true));
	// Request for access token
	if(isset($return['error'])) // if invalid output error
	{
		echo $content = 'Some error occured <br><br>'.$return['error_description'].'<br><br>Please Try again.';
	}
	else
	{
		// token received successfully
		// Get user detail
		$url = 'https://api.linkedin.com/v2/me?projection=(id,firstName,lastName,profilePicture(displayImage~:playableStreams))&oauth2_access_token='.$return['access_token'];
		$User = json_decode(curl($url),true);

		echo $UserName = $User['firstName']['localized']['en_US'].' '.$User['lastName']['localized']['en_US'];

		// Get user email id
		$url1 = 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))&oauth2_access_token='.$return['access_token'];
		$getemailurl = json_decode(curl($url1),true);

		echo $UserEmail = $getemailurl['elements'][0]['handle~']['emailAddress'];
	}
}
if(isset($_GET['error']) && $_GET['error']=="user_cancelled_authorize")
{
	echo 'Something is wrong please check';
}
You can customize this code further as per your requirement. And please feel free to ask if you have any doubt and give comments on this tutorial.
Tags: Login with LinkedIn using PHP, Simple PHP LinkedIn OAuth Login Integration, 2019 Updated Login With LinkedIn Using PHP, How to login with LinkedIn oAuth2 in PHP, Login System with LinkedIn using OAuth2 using php, How to Implement Login with LinkedIn in Website with PHP, LinkedIn OAuth2.0 Authentication Using PHP, linkedin oauth library for php, linkedin v2 api php, linkedin federated login, linkedin login usinh php, php linkedin login, wodrpress linkedin login

Author: Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.
I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts: You may like

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

Redirecting HTTP to HTTPS with PHP

Convert Base64 string to an image file

Fullscreen Background Video Using HTML5 And CSS

How to get form filed names with values php

how to get current week start date and end date in php

Extract URLs from the website using php

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.