Integrate linkedin api in your site
Posted by mahmud ahsan on March 16, 2010 in
Linkedin | 54 Comments
Now a days most popular sites provide api so that we integrate in our site to create mash-up application. Linkedin recently published their own apis. So now we can integrate linkedin apis in our site. So why we should integrate linkedin api:
- Users can bring linkedin profile and network in your site
- There are 52 millions users in linkedin so you can engage them in your site
- You may create authentication through linkedin api so that user don’t need to register in your site
- You can search profile, connection
- Users can also update their status from your site using linkedin apis
- And many more…
In this tutorial I’ll show how could you integrate linkedin api in your site, search a company name and get your profile info.
Before proceed first look at the LinkedIn APIs Terms of Use
1.
I found there is a php library So download the library. Now move the OAuth.php and linkedin.php files in your project directory.
2.
visit https://www.linkedin.com/secure/developer and click Add New Application.
Now fill the form. You have to must fill * Indicates.
Integration Url would be your project url. Here I filled http://thinkdiff.net/demo/linkedin
oAuth Redirect Url would be the web page url. In my case I provides http://thinkdiff.net/demo/linkedin/demo.php
Now click Add Application. Then you’ll get Api Key and Secret Key for your application.
Linkedin also used oAuth library, so if you don’t understand what is oAuth you can read my earlier article http://thinkdiff.net/php/develop-auto-post-publishing-twitter-app/
Now look at the project files
Now look at auth.php codes
<?php
session_start();
$config['base_url'] = 'http://thinkdiff.net/demo/linkedin/auth.php';
$config['callback_url'] = 'http://thinkdiff.net/demo/linkedin/demo.php';
$config['linkedin_access'] = 'xxxxxxxxxxxxxxYour_API_KEYxxxxxxxxxxxx';
$config['linkedin_secret'] = 'xxxxxxxxxxxxxxYour_Secret_Keyxxxxxxxxx';
include_once "linkedin.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['callback_url'] );
//$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());
?>
Now look at the demo.php codes
<?php
session_start();
$config['base_url'] = 'http://thinkdiff.net/demo/linkedin/auth.php';
$config['callback_url'] = 'http://thinkdiff.net/demo/linkedin/demo.php';
$config['linkedin_access'] = 'xxxxxxxxxxxxxxYour_API_KEYxxxxxxxxxxxx';
$config['linkedin_secret'] = 'xxxxxxxxxxxxxxYour_Secret_Keyxxxxxxxxx';
include_once "linkedin.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['callback_url'] );
//$linkedin->debug = true;
if (isset($_REQUEST['oauth_verifier'])){
$_SESSION['oauth_verifier'] = $_REQUEST['oauth_verifier'];
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->getAccessToken($_REQUEST['oauth_verifier']);
$_SESSION['oauth_access_token'] = serialize($linkedin->access_token);
header("Location: " . $config['callback_url']);
exit;
}
else{
$linkedin->request_token = unserialize($_SESSION['requestToken']);
$linkedin->oauth_verifier = $_SESSION['oauth_verifier'];
$linkedin->access_token = unserialize($_SESSION['oauth_access_token']);
}
# You now have a $linkedin->access_token and can make calls on behalf of the current member
$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,headline,picture-url)");
echo '<pre>';
echo 'My Profile Info';
echo $xml_response;
echo '<br />';
echo '</pre>';
$search_response = $linkedin->search("?company=facebook&count=10");
//echo $search_response;
$xml = simplexml_load_string($search_response);
echo '<pre>';
echo 'Look people who worked in facebook';
print_r($xml);
echo '</pre>';
?>
Now visit your site’s url in my case http://thinkdiff.net/demo/linkedin/auth.php
After login in linkedin and providing access it will redirect to http://thinkdiff.net/demo/linkedin/demo.php and you’ll see your linkedin profile information and some peoples information who worked in facebook. If you want to search other company just modify this line
$search_response = $linkedin->search("?company=your_desire_company&count=10");
You can also search name, title and many more. To see the complete list of parameters check http://developer.linkedin.com/docs/DOC-1005
To get more information about linkedin api please visit http://developer.linkedin.com/community/apis
You can also update the linkedin.php library and add new method for your purpose. For example look at the search method of this file
function search($parameters) {
$search_url = $this->base_url . "/v1/people/" . $parameters;
echo "Performing search for: " . $parameters . "\n";
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, "GET", $search_url);
$request->sign_request($this->signature_method, $this->consumer, $this->access_token);
$auth_header = $request->to_header("https://api.linkedin.com");
if ($debug) {
echo $request->get_signature_base_string() . "\n";
echo $auth_header . "\n";
}
$response = $this->httpRequest($search_url, $auth_header, "GET");
return $response;
}
So the function’s code is not too difficult to understand, right? So if you want to add invitation api http://developer.linkedin.com/docs/DOC-1012 in your project then create a new function like function invitation(){} where you’ll implement the invitation api from linkedin by copying the above code and modifying some lines.
And don’t forget to know about some limits by linkedin, regarding api calls. Visit http://developer.linkedin.com/docs/DOC-1112 to know details.
I hope you understand about the basic usage of linkedin apis and you can now integrate in your project easily.
Related Posts
If you think this article kicked ass, subscribe to the RSS feed or follow me on Twitter! Share with your friends, or leave a comment below (or better still, do both!)Comments (54)





Nice post , thanks for share
definitely a very good article. i like as you described every steps nicely
Very detailed article. However, I can’t seem to find the linkedin.php file in http://oauth.googlecode.com/svn/code/php/ Any help is appreciated. Thanks.
@Alvin, I updated the link actually download the linkedin.php and oauth.php from here
http://developer.linkedin.com/servlet/JiveServlet/download/3005-1178/LinkedInOAuth-PHP-WithSearch.zip;jsessionid=8AD7DC6FC927780812298C29BF7F2217.node0
Hi,
I want to know how linkedin can update the status message in itself and twitter at the same time. Where can I find the source code?
@Iris, you’ve to develop your own solution. You can integrate this solution with http://thinkdiff.net/php/develop-auto-post-publishing-twitter-app/ twitter solution so that you can update twitter status and linkedin status at the same time.
@ Mahmud: Thanks!
I have already gone through that but my question is what else is there apart from the four steps in that. I mean that it self is a solution right?
@Iris, you’ve to modify the codes so that is serves your purpose. Simply the logic is, you need a admin panel where you’ll first authenticate twitter and linkedin and you need a text field where you’ll put your status. After submit the status will update in linkedin and twitter. So you’ve to code what will happen after submission. I’ll keep in mind about your problem and if I make time, I’ll write a post regarding it. So tune my site.
@mahmud ahsan, thanks a lot for the link. However, right now I have an error at the LinkedIn authorization page which says “We were unable to find the authorization token”. Upon googling, one possible problem could be because my system clock is out of sync with LinkedIn’s. Any clue on how do I sync my system clock?
@Alvin, this is very weird. And I’m really not sure why this happened. One thing you can try with different browser or different Os. Sometimes the bug may happen on linkedin, I’m not sure!!!
@mahmud ahsan, I realized that when I reach the authorization step, my application does not have an authorization URL generated. Just wondering whether there’s anything I need to edit besides the base_url, callback_url, API key and secret key?
If I’m testing on localhost, is it an issue?
@Alvin, i’m not sure is the problem happened for localhost or not as I directly tested that code on server.
@mahmud ahsan, never mind. I’ll try to figure out how to solve it. Thanks for the help anyway.
@mahmud ahsan, I’ve managed to get the linkedin api working with Zend Oauth framework. However, I would still like to make use of your codes as it’s simpler to understand (and hence easier to make future modifications). I did some tests on my codes and made the following observations:
I realized that the problem lies in the request token step. I printed out the $response object in the getRequestToken() function in linkedin.php and it’s empty.
It seems that the $data object in the httpRequest() function in linkedin.php is empty as well. As such, I would like to check with you whether the parameter values that I’m passing in is correct:
$url = https://api.linkedin.com/uas/oauth/requestToken?oauth_callback=http%3A%2F%2Flocalhost%2Flinkedin2%2Fdemo.php&oauth_consumer_key=LrH1Ck7-bNVGX57WstY_HxnmsPbIN1a4OIwi-LjUnAsHKJJaks2DU6ahIa5kb5DJ&oauth_nonce=698b4ab5efafeadd382884b26fd858c4&oauth_signature=%2BRV59Bm4pZfpGP9mkWC5BCiy56k%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1269265649&oauth_version=1.0
$auth_header = Array
$method = GET
Thank you for your help.
@Alvin, I didn’t pass any parameter by myself, and couldn’t understand what the problem you’re facing. Did you run successfully my code yourself, my codes working nicely here http://thinkdiff.net/demo/linkedin/auth.php
@mahmud ahsan, I’m able to run your auth.php perfectly. The problem is that when I reach the authorization page, I am unable to retrieve a request token so the authorization url has no value for oauth_token.
@Alvin, In the application setting, did you provide the oAuth Redirect url correctly ? And also check if session is working correctly or not. some of my friends, run my codes successfully without any problem. So its really confusing why this problem occurs in your side. Are you running the code in localhost or in any server?
@mahmud ahsan, my callback url should be correct. In any case, here’s my settings
$config['base_url'] = ‘http://localhost/linkedin2/auth.php’;
$config['callback_url'] = ‘http://localhost/linkedin2/demo.php’;
Besides setting these 2 config parameters and the API key and secret, is there any other changes I need to make? I’ve not made any changes in OAuth.php and linkedin.php; is there anything I need to change in these 2 files?
I’m sure that your codes are fine, because I can run your example perfectly so I think the problem should lie with my configurations, which I’m trying to figure out where the problem lies.
I’m running the code on localhost but that shouldn’t be a problem since I can run the codes successfully on localhost using the Zend OAuth framework.
If I still can’t figure out how to solve this then I guess I’ll stick to using the Zend OAuth framework then. Anyway, thanks for your help so far.
Hi Alvin,
I am also facing the same problem as you did “We were unable to find the authorization token”.
How did you solve the problem by using Zend frameowrk. What exaclty you mean when you say you used Zend framework. Is it Server? or anything else or any change in the code..
Thanks,
Viren
@Alvin, Please run this code in server, not in your localhost. I think you’ll face no problem. There is no more changes on other files those you mentioned. May be zend oAuth library works differently than my example.
@mahmud ahsan, I guess I’ll stick to my working Zend OAuth framework then since it’s the only one I can get to work. Thanks for the help anyway. Really appreciate it.
Hello,
if I store ‘requestToken’, ‘oauth_verifier’ and ‘oauth_access_token’ in DB is possible to use it after 1,2,10 days?
regards,
@Tony, if user approve the permission as Until Revoked then you can use it for infinite time. But if user approve for one day or one week then you’ve no choice.
Hi.
Maybe I didn’t understood something, but each time after login, when I want to get profile (with specified fields, or only with ‘~’) I get the error:
I can’t investigate what is wrong, can’t make it work.
My header is:
Is there anything what I should check?
@Amores, I think the problem is something related authentication. Please visit this link http://thinkdiff.net/demo/fblinkedtwit/index.php and check if linkedin works for you or not. And the code for this project is open source checkout the article: http://thinkdiff.net/facebook/open-source-facebook-twitter-linkedin-status-update-application/
I tried running the php codes, but it does not result/output anything. I tried ‘debugging’ the auth.php file and when i
‘echo “Authorization URL: ” . $linkedin-> generateAuthorizeUrl() . “\n\n”; ‘
i get the following:
Authorization URL: https://api.linkedin.com/uas/oauth/authorize?oauth_token=
I have a feeling that $linkedin is empty, ie. the ‘ new LinkedIn() ‘ command is not generating anything. Any reasons as to why this might be happening?
@itsmeok, same answer for you. Please check this article http://thinkdiff.net/facebook/open-source-facebook-twitter-linkedin-status-update-application/ and check if linkedin status update works for you or not. If it works for you then please checkout the code of that project.
Thanks for the quick reply, mahmud!
I tried the link you gave me, the linkedin access was working on your link. However, when i checkedout the code and ran it on localhost, and click on ‘Give LinkedIn access’, it opens a blank page.
Have you faced this before? Am i doing something wrong?
@itsmeok, i think this code will not work in localhost, but I’m not sure. Normally I tested this code directly on server.
thanks.
I wan’t to create profile in my page basing on my profile from linkedIn but I can’t get my email address. Is there any way to do pass it via XML? If not – maybe I could save it in session somehow?
@amores, I don’t find email retrieving system in their api documentation http://developer.linkedin.com/docs/DOC-1002
You can save email to session as your own way.
Just to let people know if you use the above method of getting the linkedin.php file, that the setStatus function is not working correctly.
It should be:
function setStatus($status) { $status_url = $this->base_url . "/v1/people/~/current-status"; echo "Setting status...\n"; $xml = "" . htmlspecialchars($status, ENT_NOQUOTES, "UTF-8") . ""; echo $xml . "\n"; $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, "PUT", $status_url); $request->sign_request($this->signature_method, $this->consumer, $this->access_token); $auth_header = $request->to_header("https://api.linkedin.com"); if ($debug) { echo $request->get_signature_base_string() . "\n"; echo $auth_header . "\n"; } $response = $this->httpRequest($status_url, $auth_header, "PUT", $xml); return $response; }This took me a while to debug so thought i’d save others the trouble.
@Jarrod, Thanks you’ve noticed that. I also noticed that and fixed that but in another project.
Checkout my open source project.
http://thinkdiff.net/facebook/open-source-facebook-twitter-linkedin-status-update-application/
The code above seems to be working fine and I get my response in $xml_response.
Example of XML code I see when I do view source on the page is:
Kunal
……
How do I parse out data from this to display on the page?
So if I want to get First name from above and display on my page as :
First Name: Kunal
@Kunal, you can use php’s simple xml to parse xml data
Hi,
I tried it but could not get it exactly. Here is my xml when I do view source. How can I assign the firstname and lastname to varaibles so I can output it as I need on my page?
Kunal
Punwani
@Kunal, its very easy.
$xml_response = $linkedin->getProfile("~:(id,first-name,last-name,headline,picture-url,public-profile-url)"); $data = simplexml_load_string($xml_response); <table border="0" cellspacing="3" cellpadding="3"> <tr><td>Name</td> <td><a target="_blank" href="<?=$data->{'public-profile-url'}?>"><?=$data->{'first-name'}?> <?=$data->{'last-name'}?></a></td></tr> <tr><td>Headline</td> <td><?=$data->headline?></td></tr> <tr><td>Profile Image</td> <td><img src="<?=$data->{'picture-url'}?>" alt="" /></td></tr> </table>You can check my another article http://thinkdiff.net/facebook/open-source-facebook-twitter-linkedin-status-update-application/ and could check the demo to see how I show user info.
Thanks alot Mahmud Ahsan. This is very useful.
Is there a way to retrieve the Contact Settings for a user thru the LinkedIn API?
For example, I want to be able to retrieve the following:
Interested In:
career opportunities
consulting offers
reference requests
getting back in touch
@Kunal, I think currently linkedin yet not open those fields for user. I just checked from here http://developer.linkedin.com/docs/DOC-1002
Anybody had trouble running this on Yahoo! Domains?
I can’t get auth.php to do anything. There is no error msg but I can tell it stops at the ‘include_once “linkedin.php”;’ since I can echo a debug statement before that line but not after.
Thoughts?
Great post! thanks
Dear mahmud ahsan,
Thanks.. so far work!!!!!
Can you please tell me how I can re-use the access-token of a user of linked to update status without login second time at linkedin.
Like your twitter Demo…
You can save the access token in your database, but in linkedin access token has expiration date like 1 day or 2 week. So after that time your token will expire. So you’ve to cleverly code to solve this problem.
Advance thanks……..for tutorial…
… can u give the same explanation by using java………in all links they are giving in php……..but we are developing our application in java…..so please give reply………..
I don’t work on java based web application development. So I’m sorry.
Hi,
Thanks for the useful code. I followed all the steps that you mentioned in above but when I run ‘auth.php’ it gives me below message : “http 500 internal server error”.
I am using enginisite local web server.Below are the details,
$config['base_url'] = ‘http://127.0.0.1:8080/Project/auth.php’;
$config['callback_url'] = ‘http://127.0.0.1:8080/Project/demo.php’;
I am new to php so would need your help to understand where I am going wrong. Thanks..
Kind Regards,
Viren
Please use use the code in hosting server not in localhost.
Hi Mahmud,
Thanks for helping me out on my issue.
Now when I execute it , while diverting to the linkedin site it givs below error,
We were unable to find the authorization token
I have passed both the keys correctly. What may be the problem.
Thanks,
Viren
Greetings,
Mahmud, This is great article; very easy and simple to implement OAuth Linkedin.
I am using it and I am getting the profile information. Currently I am indulged in Messaging API; I want to implement it. It suppose to POST the xml.
..
I tried same as setstatus function is implemented for posting xml, but I think setstatus is working. As it is not posting xml i guess; but i can be wrong
Do you know how to do it for messaging xml? how can i post it.
Here is document link from where i m getting assistance.
http://developer.linkedin.com/docs/DOC-1044
Pardon my knowledge of PHP, I am new in it :-s
Thanks
Hi,
I tested the code on the server, but it showed error:
“My Profile Info
404
1282320837023
0
Could not find person based on: ~
Performing search for: ?company=facebook&count=10
Look people who worked in facebookSimpleXMLElement Object
(
[status] => 404
[timestamp] => 1282320837091
[error-code] => 0
[message] => Could not find person based on:
)
”
Do you know what is wrong? I didn’t change anything of the code except the consumer key, integrate url, redirect url and consumer secret key.
Thank you very much
Hi! . i am really new to this and i’ve been reading your post since i’ve started working on it… You said that I can edit it and create a new function such as the invite function. I am trying to create a sndMessage function to send message to a particular user, but it seems not to be working… Please look at my code below.. Thank you in advance.
function sndMessage($msgrecipient) {
$msg_url = $this->base_url . “/v1/people
$msgxml = “Good dayI hope you are doing well
”;
$request = OAuthRequest::from_consumer_and_token($this->consumer, $this->access_token, “POST”, $msg_url);
$request->sign_request($this->signature_method, $this->consumer, $this->access_token);
$auth_header = $request->to_header(“https://api.linkedin.com”);
if ($debug) {
echo $auth_header . “\n”;
}
$response = $this->httpRequest($msg_url, $auth_header, “POST”, $msgxml);
return $response;
}
Hi ali,
are you trying to create a function that can send message to some of your contacts? can you share it? i’m also new in php and linkedin apis..
like what did you edit? you copied the setstatus function?
thanks!!
Thank you i have got the solution
; we just need to post data in httprequest($var1,$var2,var3,$xmlbody) in callback method..
xml is below..
$bodyofxml= Congratulations! you have sent the message, now try to send it to someone else You’re certainly the best person for the job! ;
request->httprequest(,,,$bodyofxml);
and thats it
Thanks again Mehmud;
Now if you see this post, just let me know at how much extent you modified this oauth.php file from the google source code ??