Example – Login with LinkedIn using PHP


In this tutorial, we will show you how to integrate user Login System with LinkedIn using PHP. The login with LinkedIn feature helps the user to sign into your website with their LinkedIn account without registration on your website. Our LinkedIn login API tutorial provides you an easy way to build user login system with LinkedIn using PHP and store the user profile data into the MySQL database. We’ll use OAuth library to connect with LinkedIn and build LinkedIn login system in PHP.

Before you get started to integrate Login with LinkedIn using PHP, take a look at the files and folders structure.

  • src/
    • http.php
    • oauth_client.php
  • User.class.php
  • inConfig.php
  • index.php
  • logout.php
  • css/
  • images/

LinkedIn App Creation

  • Go to the Apps page at LinkedIn Developer Network and log in with your LinkedIn account credentials.
  • Click on the Add New Application link to creates new App.
  • Enter the new application details into the app registration form.
    • Company Info: If you have not already created a 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/linkedin_login_php/), Accept Redirect URL (http://localhost/linkedin_login_php/), Cancel Redirect URL (http://localhost/linkedin_login_php/).
    • Select the LinkedIn API Terms of Use checkbox.
  • Click on the Add Application button.
  • Copy and keep the Client ID and Client Secret for later use.

Database Table Creation

To store the user information from the LinkedIn database, a table (users) need to be created in MySQL database. At first, create a database (like tutspointer) and run the below SQL on the database. The following SQL creates a users table with some basic fields in the database to hold the LinkedIn profile information.

LinkedIn OAuth Library for PHP

The src/ directory contains the LinkedIn OAuth client library for PHP. You don’t need to download it separately, all the required files are included in our source code.

User Class (User.class.php)

The User class helps to insert or update user data to the database using PHP and MySQL. In User.class.php file, you only need to specify your MySQL database credentials ($dbHost, $dbUsername, $dbPassword, and $dbName) and table name ($userTbl) where you want to store the user’s LinkedIn profile information.

LinkedIn API Configuration (inConfig.php)

In inConfig.php file, specify your LinkedIn Client ID ($apiKey), Client Secret ($apiSecret), Callback URL ($redirectURL), and Permissions ($scope) to connect with LinkedIn API and working with OAuth library.

Note that: You’ll find the Client ID and Client Secret on your LinkedIn Apps Authentication page.

Login & Get Profile Information (index.php)

Initially, the Sign in with LinkedIn button will be shown. Once the user authenticates with their LinkedIn account, the profile information will be fetched and pass to the User class for inserting into the MySQL database. Also, the LinkedIn profile details with logout button will be shown to the user.

Logout (logout.php)

When the user wishes to log out from their account, the user would be redirected to the logout.php file.


Leave A Reply

Your email address will not be published.