Login with LinkedIn using PHP


We have been published Facebook, Twitter and Google login authentication tutorial in our previous posts. Today we will discuss about one of the popular social network login oauth in PHP. In this tutorial you can learn how to integrate login with LinkedIn using PHP. We have developed simple PHP script for login with LinkedIn. LinkedIn login API script is used oauth library to connect with LinkedIn in PHP.
Our LinkedIn login API tutorial provides you an easy way to build login with LinkedIn using PHP. You can store LinkedIn user data at the database using MySQL. Let’s start the LinkedIn oAuth PHP tutorial.
You can check the live demo of LinkedIn login API with PHP from the above Demo link and also able to download the complete project from the above Download link.

login-with-linkedin-using-php-by-codexworld

This project has three folders named LinkedIn/ includes/ and some PHP files. The folders and files structure are given below:

  • LinkedIn/
    • http.php
    • oauth_client.php
  • includes/
    • db.php
  • index.php
  • config.php
  • process.php
  • logout.php

LinkedIn App Creation:

Go to the Apps page at LinkedIn Developer Network and login with your LinkedIn account credentials – https://www.linkedin.com/secure/developer
 Click on the Add New Application link for create new App.
 Enter the new application details into the app registration form.
 Company Info: If you already have not any company page, select “New Company” and enter your company name.
 Application Info: Enter all the application details and select “Live” option as “Live Status”.
 Contact Info: Enter your contact details.
 OAuth User Agreement: Select the “Default Scope” and enter “Redirect URLs” (http://localhost/login_with_linkedin_using_php/process.php), “Accept Redirect URL” (http://localhost/login_with_linkedin_using_php/process.php), “Cancel Redirect URL” (http://localhost/login_with_linkedin_using_php/index.php).
 Select the LinkedIn API Terms of Use checkbox.
 Click on the Add Application button.
 Once the application is created successfully, application details would be displayed.
 Keep the API Key and Secret Key for later use.

Database table creation:

To store the user information, you need to create a database and a table. At first create a database like codexworld. After that copy the below SQL query and run the SQL query on the database.

CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `oauth_provider` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `oauth_uid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `fname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `lname` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `location` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `country` varchar(50) COLLATE utf8_unicode_ci NOT NULL, 
    `picture_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `profile_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `created` datetime NOT NULL,
    `modified` datetime NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Project Configuration:

In this project you just have to modify only two files.
 Database configuration (includes/db.php): db.php file have DB class with some functions. This DB class is used for managing the LinkedIn user data into the database. Into the dbConnect() function we have configured the database and connect with the database. You need only to change the $dbServer, $dbUsername, $dbPassword and $dbName variables value with your MySQL server details.

function dbConnect(){
    
// database configuration
    
$dbServer 'localhost';
    
$dbUsername 'root';
    
$dbPassword '';
    
$dbName 'codexworld';
    
$conn = new mysqli($dbServer,$dbUsername,$dbPassword,$dbName);
    if(
$conn){
        
$this->db $conn;
    }else{
        die(
"Database conection error: ".$conn->connect_error);
    }
}

 LinkedIn API configuration (config.php): open the config.php file and modify the $baseURL, $callbackURL, $linkedinApiKey and $linkedinApiSecret variables value.
Note that: You can find the Authentication Keys from your LinkedIn Apps details page.

<?php
$baseURL 
'http://localhost/login-with-linkedin-using-php/';
$callbackURL 'http://localhost/login-with-linkedin-using-php/process.php';

// LinkedIn Authentication Keys
$linkedinApiKey 'LinkedIn Client ID';
$linkedinApiSecret 'LinkedIn Client Secret';

// LinkedIn Application Permissions
$linkedinScope 'r_basicprofile r_emailaddress';
?>

 If you have any query about this tutorial and scripts, feel free to comment here.

14 Comments

  1. Gurmukh Singh Said...
  2. Steve Jeong Said...
    • CodexWorld Said...
  3. Aniket Said...
  4. Samuel Said...
  5. Tiffany Said...
  6. Ashu Said...
  7. Ashu Said...
  8. Deepanshu Said...
  9. Usman Said...
  10. Usman Said...
  11. Usman Said...
    • CodexWorld Said...

Leave a reply

Connect With CodexWorld