The Right way to Login With LinkedIn using API in PHP, create Login With LinkedIn easily, tutorial & example PHP script for download.

How To Create Login With LinkedIn In PHP

Very easy, using LinkedIn API and Qassim_HTTP() Function, we will create login with LinkedIn.

Create LinkedIn App

Go to LinkedIn App and create a new App, we need “client_id” and “client_secret” and “redirect_uri”.

login with linkedin api php

Create Login With LinkedIn

Now using LinkedIn App, we will create login with LinkedIn, we need 6 PHP files:

  1. config.php
  2. login.php
  3. Qassim_HTTP.php
  4. callback.php
  5. index.php
  6. logout.php

Create new PHP file “config.php” and enter this code inside it:

<?php

    // Script By Qassim Hassan, wp-time.com

    // go to https://www.linkedin.com/developer/apps and create a new app

    $client_id = "xxxxxx"; // enter your client id

    $client_secret = "xxxxxx"; // enter your client secret

    $redirect_uri = "http://qass.im/login-with-linkedin/callback.php"; // enter your redirect url

?>

Create new PHP file “login.php” and enter this code inside it:

<?php

    session_start();

    // Script By Qassim Hassan, wp-time.com

    if( isset($_SESSION['user_info']) ){ // check if user is logged in
        header("location: index.php"); // redirect user to index page
        return false;
    }

    include 'config.php'; // include app info

    $_SESSION['login'] = 1;

    header("location: https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=$client_id&redirect_uri=$redirect_uri&state=CSRF"); // redirect user to oauth page

?>

Now create new PHP file “Qassim_HTTP.php” and enter this code inside it:

<?php

/* Function By Qassim Hassan, wp-time.com */
function Qassim_HTTP($method, $url, $header, $data, $json){
if( $method == 1 ){
$method_type = 1; // 1 = POST
}else{
$method_type = 0; // 0 = GET
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);

if( $header !== 0 ){
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}

curl_setopt($curl, CURLOPT_POST, $method_type);

if( $data !== 0 ){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}

$response = curl_exec($curl);

if( $json == 0 ){
$json = $response;
}else{
$json = json_decode($response, true);
}

curl_close($curl);

return $json;
}

?>

Now create new PHP file “callback.php” and enter this code inside it:

<?php

    // Script By Qassim Hassan, wp-time.com

    session_start();

    if( isset($_SESSION['user_info']) or !isset($_SESSION['login']) ){ // if user is logged in
        header("location: index.php"); // redirect user to index page
        return false;
    }

    include 'Qassim_HTTP.php'; // include Qassim_HTTP() function

    include 'config.php'; // include app data

    $code = $_GET['code'];

    /* Get User Access Token */

    $method_ = 1; // method = 1, because we want POST method

    $url_ = "https://www.linkedin.com/uas/oauth2/accessToken";

    $header_ = array( "Content-Type: application/x-www-form-urlencoded" );

    $data_ = http_build_query( array(
        "client_id" => $client_id,
        "client_secret" => $client_secret,
        "redirect_uri" => $redirect_uri,
        "grant_type" => "authorization_code",
        "code" => $code
    ));

    $json_ = 1; // json = 1, because we want JSON response

    $get_access_token = Qassim_HTTP($method_, $url_, $header_, $data_, $json_);

    $access_token = $get_access_token['access_token']; // user access token

    /* Get User Info */

    $method = 0; // method = 0, because we want GET method

    $url = "https://api.linkedin.com/v1/people/~:(id,num-connections,picture-url,email-address,first-name,last-name,picture-urls::(original))?format=json"; // read about field: https://developer.linkedin.com/docs/fields/basic-profile

    $header = array("Authorization: Bearer $access_token");

    $data = 0; // data = 0, because we do not have data

    $json = 1; // json = 1, because we want JSON response

    $user_info = Qassim_HTTP($method, $url, $header, $data, $json);

    $_SESSION['user_info'] = $user_info; // save user info in session

    header("location: index.php"); // redirect user to index page

?>

Now create new PHP file “index.php” and enter this code inside it:

<?php

    // Script By Qassim Hassan, wp-time.com

    session_start();

    if( isset($_SESSION['user_info']) ){ // if user is logged in
        $user_info = $_SESSION['user_info'];
        ?>
            <h3>Welcome <?php echo $user_info["firstName"]; ?> <?php echo $user_info["lastName"]; ?> !</h3>
            <p><img src="<?php echo $user_info['pictureUrls']['values'][0]; ?>"></p>
            <p>Connections: <?php echo $user_info["numConnections"]; ?></p>
            <p>Your Email: <?php echo $user_info["emailAddress"]; ?></p>
            <p><a href="logout.php">Logout</a></p>
        <?php
    }

    else{ // if user is not logged in
        echo '<a href="login.php">Login With LinkedIn</a>';
    }

?>

Finally, create new PHP file “logout.php” and enter this code inside it:

<?php

    session_start();

    /* Script By Qassim Hassan, wp-time.com */

    // if user is logged in, destroy all facebook sessions
    if( isset($_SESSION['user_info']) or isset($_SESSION['login']) ){
        unset( $_SESSION['user_info'] ); // destroy
        unset( $_SESSION['login'] ); // destroy
        header("location: index.php"); // redirect user to index page
    }

    else{ // if user is not logged in
        header("location: index.php"); // redirect user to index page
    }

?>

Done.

Login With LinkedIn Demo

Check live demo.

Download

Download login with LinkedIn PHP Script and Live Demo example.

More

Check more Tutorials of Login with API.