Authenticate user with LinkedIn API from your PHP application

1 minute read

Recently I wanted to let my users authenticate with LinkedIn. We had just added a Facebook and Twitter authentication, so why not LinkedIn. To my big surprise there were no LinekdIn API clients for PHP. At least no good ones. And the code samples from developer.linkedin.com where lookting pretty nasty. So, as a good open source developer I decided to build one and share it.

My LinkedIn API client

With inspiration from the Facebook PHP SDK I came up with a solution that hides all the nasty stuff from the code samples. Click here to review and download my LinkedIn PHP Api client. I wanted to focus on these three things:

  1. Easy to implement
  2. Easy to extend
  3. Easy to test

So how do you use it? Study this code example and tell me in the comments what you think:

<?php
//index.php


//make sure you have included composers autoload.php before you begin.
$linkedIn=new HappyR\LinkedIn\LinkedIn('app_id', 'app_secret');


//if not authenticated
if (!$linkedIn->isAuthenticated()) {
$url = $linkedIn->getLoginUrl();
echo "<a href='$url'>Login with LinkedIn</a>";
exit();
}


//we know that the user is authenticated now
$user=$linkedIn->get('v1/people/~:(firstName,lastName)');


echo "Welcome ".$user['firstName'];

Categories:

Updated:

Leave a Comment