How to login with LinkedIn oAuth2 in PHP and MySQL
Mysql 20-May-2017

How to login with LinkedIn oAuth2 in PHP and MySQL

LinkedIn is a business oriented social networking platform and used for professional networking. We create a tutorial on login with LinkedIn oAuth which is not working properly and difficult to configure for developers, after receiving many complains from rea

Let’s start.

Step 1: Goto and click Create Application button.

step 1 LinkedIn login in PHP and MySQL

Step 2: Fill all information of your application.

step 2 LinkedIn login in PHP and MySQL

Press Submit button it will redirect you to the application page.

Step 3: Add callback URL on application page.

step 3 LinkedIn login in PHP and MySQL

Press Update and you are done with application settings.

Now Move the the database section

Create users table follow structure below:

ders I am writing this new tutorial on LinkedIn oAuth2 it’s super easy to integrate with your website in few simple steps.

-- Database: `linkedin`

-- --------------------------------------------------------

-- Table structure for table `users`

  `userid` varchar(30) NOT NULL,
  `firstName` varchar(100) NOT NULL,
  `lastName` varchar(100) NOT NULL,
  `emailAddress` varchar(100) NOT NULL,
  `position` varchar(200) NOT NULL,
  `location` varchar(40) NOT NULL,
  `profileURL` varchar(200) NOT NULL,
  `pictureUrls` text NOT NULL,
  `headline` text NOT NULL,
  PRIMARY KEY (`id`)

PHP Code

define('DB_SERVER', 'localhost'); // Database server
define('DB_USERNAME', 'username'); // Database Username
define('DB_PASSWORD', 'password'); // Database Password
define('DB_DATABASE', 'database'); // Database Name
$connection = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); // connecting with database

Edit above file as per your database configuration.



$config['callback_url']         =   ''; //Your callback URL

$config['Client_ID']      =   ''; // Your LinkedIn Application Client ID
$config['Client_Secret']      =   '';  // Your LinkedIn Application Client Secret


Edit config file and add you application information. check application page step 2.


Include config.php and db.php files in index file
Below code will verify your configuration.


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=""></a>';

Login button section
echo '<a href="'.$config['Client_ID'].'&redirect_uri='.$config['callback_url'].'&state=98765EeFWf45A53sdfKef4233&scope=r_basicprofile r_emailaddress"><img src="./images/linkedin_connect_button.png" alt="Sign in with LinkedIn"/></a>';
?>Callback code section on index.php file


if(isset($_GET['code'])) // get code after authorization
    $url = ''; 
    $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(post_curl($url,$param),true)); // Request for access token
    if($return['error']) // if invalid output error
       $content = 'Some error occured<br><br>'.$return['error_description'].'<br><br>Please Try again.';
    else // token received successfully
       $url = ',firstName,lastName,pictureUrls::(original),headline,publicProfileUrl,location,industry,positions,email-address)?format=json&oauth2_access_token='.$return['access_token'];
       $User = json_decode(post_curl($url)); // Request user information on received token
      // Insert Data in Database
       $query = "INSERT INTO `test`.`users` 


First off all we check if code received or not, using that code we will request for access_token once we get token we can get users information like name, email, picture and many more. In last step store that data in database.

That’s it all done and you have a output looks like this: