December 29, 2023 5:01 am

How to Integrate login with LinkedIn oAuth in PHP

Now a days popular sites provide api so that we can integrate in our sites and get there information on one click no more fill complex forms etc. Today i am going to explain you how to integrate with LinkedIn one of the most popular website.

LinkedIn

Follow new Tutorial: How to login with LinkedIn oAuth2 in PHP and MySQL

Make New Application on LinkedIn:
1.visit https://www.linkedin.com/secure/developer and click Add New Application.

Fill all information of your application.

 

You will get linkedin API key and Secret Key

Programming Time:

The script contains two folders called oAuth and images with PHP files.

oauth

– class.linkedClass.php // Class Get user Information

– config.php // Configuration file

– linkedinoAuth.php // Call methods

– OAuth.php // Twitter OAUTH library

images

linkedinauth.php //Call back page Show details

html.inc // html design view

index.php // Main index file show data

linkedin.php // Redirect to linkedin for authorization

 PHP Code

Edit config.php

<?php

$config['base_url']             =   'http://demo.phpgang.com/'; 
$config['callback_url']         =   'http://demo.phpgang.com/index.php';
//linkedin configuration
$config['linkedin_access']      =   'your_linkedin_access';
$config['linkedin_secret']      =   'your_linkedin_secret'; 
$config['linkedin_library_path']=   'linkedinoAuth.php';
?>

Index.php

Shows a button for linkedin authorization if not authorized.

Redirect you to linkedin.php generate token and redirect to linkedin app authorization page.

<?php
    session_start();
    include_once 'oAuth/config.php';
    include_once 'oAuth/linkedinoAuth.php';

    # First step is to initialize with your consumer key and secret. We'll use an out-of-band oauth_callback
    $linkedin = new LinkedIn($config['linkedin_access'], $config['linkedin_secret'], $config['base_url'] . 'linkedin_login_oauth/linkedinauth.php' );
//    $linkedin->debug = true;

    # Now we retrieve a request token. It will be set as $linkedin->request_token
    $linkedin->getRequestToken();
    $_SESSION['requestToken'] = serialize($linkedin->request_token);

    # With a request token in hand, we can generate an authorization URL, which we'll direct the user to
   ## echo "Authorization URL: " . $linkedin->generateAuthorizeUrl() . "\n\n";
    header("Location: " . $linkedin->generateAuthorizeUrl());
?>

Once you Allow access it will redirect you on linkedinauth.php (defined in linkedin.php) and show all data of authorized user.

 

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:

89 responses to “How to Integrate login with LinkedIn oAuth in PHP”

  1. I want download source code but i can’t because when i type and press subscribe, after that i come back to download page and when i clicked download message says: Sorry no email found subscribe below. And also where can i know about my email id?

  2. the problem is solved, i downloaded already

  3. Yoyo says:

    Hi, sorry for my english, but i have a problem, my token doesn’t exist …
    This is my linkedin Object:

    LinkedIn Object
    (
    [base_url] => http://api.linkedin.com
    [secure_base_url] => https://api.linkedin.com
    [oauth_callback] => xxxxx
    [consumer] => OAuthConsumer Object
    (
    [key] => xxxxxxxxxxxx
    [secret] => xxxxxxxxxx
    [callback_url] => xxxxx
    )

    [request_token] =>
    [access_token] =>
    [oauth_verifier] =>
    [signature_method] => OAuthSignatureMethod_HMAC_SHA1 Object
    (
    )

    [request_token_path] => https://api.linkedin.com/uas/oauth/requestToken?scope=r_fullprofile r_emailaddress r_contactinfo
    [access_token_path] => https://api.linkedin.com/uas/oauth/accessToken
    [authorize_path] => https://api.linkedin.com/uas/oauth/authorize

    Someone can help me please??

  4. Jols Bardz says:

    I already sign up my email to download, but still I can’t download.. help

    • huzoorbux says:

      @jolsbardz:disqus List update after every 18 hours now you are in the list and go to download scrip.

      Feel free to contact us.

  5. Hery says:

    When i have login with linkdin application does not fetch email id or user info and does not create it wordpress user…

    • huzoorbux says:

      What error you are getting in your script please paste below.

      • Hery says:

        Sir

        After login with linkdin the website return on this

        link:-www.demo.com/linkedin_login_oauth/linkedinauth.php?oauth_token=d3dd2e7c-b584-4115-bc52-81e895dff24b&oauth_verifier=78846..

        But i want to know login user name ,password and email to register it with wordpress…..

        • huzoorbux says:

          send me the page link where you put your linkedin button and please make sure that you have changed you application ids in config.php.

          • Hery says:

            sir

            I have try to solve the problem but i got “unauthorized” message after login with linkdin:-

            401
            1379409071935
            YT9I6MG03L
            0
            [unauthorized]. The token used in the OAuth request is not valid.

          • sunil says:

            sir

            I have try to solve the problem

            Array
            (
            [error] => Array
            (
            [status] => Array
            (
            [value] => 400
            )

            [timestamp] => Array
            (
            [value] => 1408517529237
            )

            please give me solution as fasr as u can

            [request-id] => Array
            (
            [value] => 3MLDYSR4PE
            )

            [error-code] => Array
            (
            [value] => 0
            )

            [message] => Array
            (
            [value] => Malformed path segment {:(people:(id,first-name,last-name,picture-url,site-standard-profile-request,headline),num-results)https://api.linkedin.com/v1/people-search}
            )

            )

            )

  6. Hery says:

    Hello

    How i can got the user profile for create it wordpress user…..

  7. sayf says:

    Hi this a amazing application, thanks.
    I just wanna know how to get the fields’ titles?

  8. sayf says:

    Hi this a amazing application, thanks.
    I just wanna know how to get the fields’ titles?

  9. sayf says:

    Please Can someone help me to find out how to retrieve fields’ titles?

    • huzoorbux says:

      @disqus_6zfMYmlyRQ:disqus it return data in xml you have to parse xml or convert it to array chose the easy steps. I have updated code please download fresh copy and use it now it will return data in array.

  10. siddhu says:

    code not working getting error please help me sir “We were unable to find the authorization token”

  11. binil billu says:

    getting error like The requested URL /linkedin_login_oauth/linkedinauth.php was not found on this server.

  12. binil billu says:

    i want to insert the linked in details to database . how can i do that?

  13. Ajitesh says:

    https://www linkedin com/uas/oauth/authorize?oauth_token=

    We were anable to find authorization token. How to fix this ?

    If it is about hte server time and linkedin time sync problem, the how do we fix that ?

  14. Manisha Gaidhane says:

    i am getting error “Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in D:xampphtdocslinkedin_login_oauthindex.php on line 3” please suggest me way to solve this.

  15. Narasimha says:

    token nor returning

  16. Sree Hari says:

    hi,
    Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in C:xampphtdocslinkedin_login_oauthindex.php on line 3

  17. Deepak says:

    Sir,

    Can you please tell me how can i set the logo?

    Thanks
    Deepak

  18. Marc says:

    Downloaded the exact Demo coded linked here on the website and getting this error:
    Notice: Use of undefined constant CONSUMER_SECRET – assumed ‘CONSUMER_SECRET’ in C:xampphtdocslinkedin_login_oauthindex.php on line 3
    And if I press on the login button I’m not getting a token. Any way to fix this?

  19. Prashant Pawar says:

    Showing error
    Call to undefined method SimpleXMLElement::count() in linkedin_login_oauth/linkedinauth.php on line 35

  20. saurav daga says:

    An Error occurred during authorization, please try again later.where do i need to change the server time so that this code can work

  21. taskeen says:

    Fatal error: Call to undefined function curl_init() in C:wampwwwlinkedin_login_oauthlinkedin_login_oauthoAuthlinkedinoAuth.php on line 117Call Stack
    plz help me

  22. oussama says:

    HI @huzoorbux:disqus please can you help me i have this error
    Fatal error: Call to undefined method SimpleXMLElement::count() in C:wampwwwphp_linkedin1linkedinauth.php on line 43

  23. wasim pathan says:

    Array
    (
    [error] => Array
    (
    [status] => 401
    [timestamp] => 1399898567394
    [request-id] => IFLFGOB9CB
    [error-code] => 0
    [message] => [unauthorized]. No consumer found for key /
    )

    )

    When I am integration your code with my Yii application it is giving me the above array in response.
    Using the same key and secret when I run your code it works properly please help.

  24. Niranjan Singh Chouhan says:

    when i am clicking on cancel button it is redirect on index page. How do i remove this bug?

  25. minu says:

    i cant download script

  26. Xuan Vichia says:

    Fatal error: Call to undefined method SimpleXMLElement::count()

  27. Ravi Singh says:

    Fatal error: Call to undefined method SimpleXMLElement::count() in linkedinauth.php

  28. Vanishri says:

    Hi,
    I am getting below output

    Array
    (
    [error] => Array
    (
    [status] => 401
    [timestamp] => 1412938920976
    [request-id] => QCS4ADRKM1
    [error-code] => 0
    [message] => [unauthorized]. The token used in the OAuth request is not valid. consumerKey: 75s75k5nxrkvxp
    )

    )

    Please help me what might me the reason .

  29. binil billu says:

    hi when i am implement the code getting error like below
    An Error occurred during authorization, please try again later. and the code failed to generate the oauth token.

    is there any problem with the http/https iam testing with my local server.
    please help.

    • Brandon DeWitt says:

      If you’re still interested, take a look at my reply comment under @huzoorbux:disqus’s last comment about 2 weeks ago (should be at the top of this post’s thread)

  30. Sushil Kumar says:

    I am getting this error : – An Error occurred during authorization, please try again later.

    Ccn you please help me to figure out the issue?

  31. Deena says:

    Hi ,this code not working for me.I have following error

  32. Manoj Rammurthy says:

    hello i am able to generate all the required fields , so if i have to make another call with the same linkedin credential how to make it , how to store access token from your library please do the favour

  33. kamal says:

    can any body tell me how to set these files
    and where to place which file in which folder of codigniter
    can anybody help that how and which file will placed into which folder ?

    • Huzoor Bux says:

      open linkedin.php
      in the httpRequest function add:
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

      I also changed ubuntu timezone to EST using follow command:

      exec(“ln -sf /usr/share/zoneinfo/EST localtime”); // run this command in a new file and run on your server.

      This erros should be fixed and linkedin should return request token.

    • Huzoor Bux says:

      open linkedin.php
      in the httpRequest function add:
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

      I also changed ubuntu timezone to EST using follow command:

      exec(“ln -sf /usr/share/zoneinfo/EST localtime”); // run this command in a new file and run on your server.

      This erros should be fixed and linkedin should return request token.

  34. Brandon DeWitt says:

    Please see my reply above. This should solve your issue.

  35. Brandon DeWitt says:

    Looks like my comment has to be approved.. It was kinda lengthy with a photo attached.

  36. Brandon DeWitt says:

    After going a little further into the code – since I continued to get error messages. I noticed that in the same file (oAuth/linkedinoAuth.php) there were a couple of php errors that said a reference variable ‘debug’ was not set.

    Change lines 57 & 73 to read if($this->debug), rather than just if($debug). For whatever reason, even though it was listed as a public variable at the beginning of the class, it wasn’t pulling through. This seems to have fixed this issue.

    I think the wise choice for folks who are looking to use this is to store the token & secret that you receive from LinkedIn in a database of some kind or a session variable, because if you refresh the linkedinauth.php page after you’ve already logged in, it no longer has a trace of the token & secret – its only available the first time this page processes/loads and then you’ll have to go back to the log in page an do log in again. This causes two errors for unset variables ‘oauth_token’ & ‘oauth_token_secret’, which again would not be thrown if the script checked a DB or Session variable and if not set, redirect back to login page.

    I also kept getting an error on line 121 in oAuth/linkedinoAuth.php – You have a comment listed as // Set the headers. Anything that I try and change this too, it kills the login and return of user information. I simply put the @ symbol in front of the line and kept it moving. Now it seems the script is working exactly as it should! 🙂

    @huzoorbux:disqus you’re the man! Even though I’ve had to troubleshoot the code a bit, this is a great script!

    • Brandon DeWitt says:

      You already have session variables! Awesome! 🙂

      But in the linkedinauth.php file line 13 should read:

      if (isset($_REQUEST[‘oauth_verifier’]) AND !isset($_SESSION[‘oauth_access_token’])){

      rather than:

      if (isset($_REQUEST[‘oauth_verifier’])){

      Or it just tries to get another access token, but LinkedIn doesn’t like this and throws this error with the variable returned: [message] => [unauthorized]. The token used in the OAuth request is not valid.

  37. karthick says:

    I am getting this error An Error occurred during authorization, please try again later.when integrating linkedin signup in website(localhost).Please provide solution asap.

Leave a Reply

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