April 21, 2016 10:50 am

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 readers I am writing this new tutorial on LinkedIn oAuth2 it’s super easy to integrate with your website in few simple steps.How to login with LinkedIn oAuth2 in PHP and MySQL

Let’s start.

Step 1: Goto https://www.linkedin.com/developer/apps/ 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:

PHP Code

db.php

Edit above file as per your database configuration.

config.php

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

index.php

Include config.php and db.php files in index file

Below code will verify your configuration.

Login button section

Authorize LinkedIn mysql & PHP

Callback code section on index.php file

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:

LinkedIn OAuth 2 in PHP & mysql final outcome

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • jason bourne

    I recently worked on linkedin api. you can only get basic information through linkedin api. If you want to get full profile of the user through oauth, you have to submit your application to linkedin and tell them reasons why you need full profile access. If the approve your application, you can get full profile otherwise not.

  • tomas nemecek

    Hello, nice example. Have you an advice, how to call linkedIn api using oAuth2 server-to-server (without login window)? We want to get count of likes and comments from our profile and save it to DB. I think, that standard oAuth2 implementation allows to set “grant_type” url parameter to value “client credentials” or “password” for testing and server to server communication. But LinkedIn supports only “authorization_code” and not others. Please, have you some idea, how to solve this?

    • You can store user’s access_token for future use token’s validity is 60 days after 60 days you can renew token.

  • Alok Deo

    I am trying to implement this in codeigniter framework. I m getting the blank details i guess the curl function is not returning the token.
    Could you plz help me?
    i Have already enabled the CURL

    • try to debug line by line. This code is super simple to implement.

  • sradha

    Thank you ,it helps a lot.

  • sradha

    Thank you for your support ,i have used it in my cakephp framework,its also working fine.

  • Razor

    For me return this error:

    Fatal error: Call to undefined function post_curl()

    The line of code is: $return = (json_decode(post_curl($url,$param),true)); // Request for access token

  • Soumya

    please reply why locally it returns null

  • Oliver Tellnes

    The method post_curl() is missing, if you download the script it will show at the bottom of the index-file, it is as follows:
    function post_curl($url,$param=””)
    {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    if($param!=””)
    curl_setopt($ch,CURLOPT_POSTFIELDS,$param);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
    }

  • Tony Harris

    Link not working to download code. Keep getting “Sorry no email found subscribe below.”

  • venkatanarayanan

    Im getting errors undefined variable id, firstname, lastname, etc..

  • Mohd Danish Yusuf

    Hi Huzoor Bux,
    I am using linkedin API to get user data but not returning full profile data. is there any authorization required by linkedin?

  • Akash Bhiwgade

    $User has NULL value.. why??