Use the LinkedIn PHP Class to integrate LinkedIn into your website. This Tutorial shows you how step by step. In this first part I'll explain how to set up a connection with the LinkedIn API and show you how you can fetch a basic profile. In the next parts I'll explain how you can post network and status updates.
OAuth and the LinkedIn PHP Class
The LinkedIn PHP Class uses the OAuth extension of PHP. This extension can be installed via PECL. More information about the installation can be found here: LinkedIn Developer Network of Installation of Pecl extensions.
Before you start you need to download the LinkedIn PHP Class, unzip the file and upload linkedin_v4.php to your server. Include the file where you want to use it require_once('/pad/naar/linkedin_v4.php').
- Download linkedin_v4.php (zip, 8kb)
Step 1: LinkedIn Object
You start by creating a new linkedin object. The linkedin object needs your apiKey and secretKey to connect with LinkedIn. If you don't have an apiKey yet, you can get it here. The callback is optional.
$apiKey = '--apikey--'; $secretKey = '--secretkey--'; $callback = 'http://voorbeeld.nl/linkedin/test'; $linkedin = new Linkedin($apiKey,$secretKey,$callback);
Authorization procedure
The code above doesn't do anything yet. When you make your first request the class will authorize via OAuth. In simpel words the authorization via OAuth works as follows:
- A request token is retreived from LinkedIn and the user will be redirected to an authorization page at LinkedIn where he will be asked to enter his username (email) and password. LinkedIn then knows the user grants you permission to retreive his information via the API.

- LinkedIn sends the user back to the given callback url. The class now retreives an acces token. You'll need the access token to do requests.
- With each request a signature string will be generated based on the apiKey, secretKey and the access token. Without this signature string the request can't be done.
GET, PUT and POST
There are three different methods to retreive and send information to LinkedIn. GET, PUT and POST. GET will get you information (e.g. your profile). PUT is used to update something that is already there (e.g. your status) and POST is used to create something new (e.g. a network update, comment or message). These methods are simplified by the LinkedIn PHP Class.
Step 2: Basic profile
Lets start by retreiving your basic profile.
// Basic profile information without extra options $profile = $linkedin->getData();
$linkedin->getData() retreives the basic profile of a user. LinkedIn returns XML, but the class transforms this in a workable PHP Array. $profile now contains:
Array
(
[first-name] => Jeroen
[last-name] => Sentel
[headline] => Internet ( Business ) Developer
[site-standard-profile-request] => Array
(
[url] => http://www.linkedin.com/profile?viewProfile=&key=12458484&authToken=zdHN&authType=name
)
)
Connections and Network Updates
In Part 2 I'll further explain the options that can be used with getData() and fetching network updates and connections. Untill then you could try out the class yourself. The comments in the file will explain a lot.
Also see the LinkedIn API documentatie.
Join my LI API Developers Group on LinkedIn.
Comments or suggestions can be posted below.
Related Posts
- LinkedIn API PHP Class Part 1: Getting Started Tutorial
- Get Some Shiny Stats From LinkedIn
- LinkedIn API 'Throttle Limits'
- LinkedIn open voor externe ontwikkelaars
- LinkedIn en Twitter gaan samenwerken
- Codebase online soon!
- Vacature: CakePHP Developer
- 14 Tips voor Blog Comment Design
Comments
Eric
Dec 31st 2009, 09:55
Great work Jeroen (have my first linkedin request working in less than 5 minutes!) and thanks for your english tutorial ;)
Eric
Dec 31st 2009, 12:16
Me again.
I'have do some test and have encounter a little problem. When you have by example one education entry the XMLToArray function return an array like this :
Array [2]
@attributes Array [1]
education Array [8]
In education we have the values for the education. If we have two educations there will be an array.
Not a big problem and I know that without additional informations the xmlToArray function can't guess easily which data need to be flatten as array or not.
I was wondering if using the attributes total could be a solution for this? (it seems that linkedin use this attribute for all collection).
Eric
Dec 31st 2009, 12:33
I try a little patch on XMLToArray function. Maybe not the best way to do it.
function XMLtoArray($obj) {
$obj = is_string($obj) ? simplexml_load_string($obj) : $obj;
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
if(!empty($_arr)) {
foreach ($_arr as $key => $val) {
$val = (is_array($val) || is_object($val)) ? $this->XMLtoArray($val) : $val;
$arr[$key] = $val;
}
if (isset($arr['@attributes']['total']) && $arr['@attributes']['total'] == 1) {
if (count($arr) == 2) {
foreach($arr as $key => $value)
if ($key != '@attributes') {
$arr[$key] = array($value);
}
}
}
return $arr;
}
}
Eric
Jan 6th, 10:08
Cool news to heard.
I'have made some other changes on your class (like add an array of error message instead of echo it, add an exit; after the header('location') but not sure it's the correct way...), if you need some patch for your next version I can provide one.
And I was seeking for a problem I encountered during my test : if someone is ask to authenticate on linkedin website and don't do it, after I have error message (like "401 : The request token used hasn't been authorized by user."). I have to clean up session variables to repair it, but sometime even this tips don't work but I don't see why.
I don't know if it's come from your class or from my code / patch to your class.
But very good joob Jeroen, it's help me a lot.
Thank
Jeroen Sentel
Jan 5th, 21:24
Hi Eric,
Thanks for your replies. I haven't spotted this yet, thanks for that!
Your solution makes perfect sense. I'm not sure yet to implement it because I strive to separate the logic that 'talks' with LinkedIn and the logic that interprets and processes the results.
Grtz, Jeroen
Jeroen Sentel
Feb 5th, 09:42
Hi Muthukumar,
Here's the beginners guide to oauth or maybe you could use the pear download.
http://hueniverse.com/oauth/
http://pear.php.net/package/HTTP_OAuth

Internet (business) developer at Alex van Groningen where I am responsible
for the development and continuation of our internet businessline. I am also particularly
interested in everything around SEO, Usability, Business Models, Online Communities, Networking
and much more.
muthukumar
Feb 5th, 05:03
Hi All,
You have succuesfully got the output but I want to use the same class. I want the OAuth class that you have used since I am getting the same error.
I am new and want to get this dine since I am at my client place trying to figure out a way to use search API. Please one of you'll can let me know how can I get this Search API implemented.
Please any help will be appreciated since I have been researching and trying yout ways but no success yet.
please help since i want this to be used which is more simple. please tell me how can I get the OAuth class.
Thanks