In this article, we are going to learn how you can get user’s data with LinkedIn API using PHP. There are a couple of social networks which are popular around the world. Sign in with social media is a trendy module on the Internet.
Nowadays website visitors or users are getting lazier and lazier, the most uncomfortable activity on the Internet is probably “Creating The Account“.
So we need to think about our visitor’s comfort first to make them happier. Because visitors are the most important thing to grow an online business.
There are many popular social sites available which provide API to share their user’s data to make such things easier and comfortable. Such social sites are Facebook, Google, Twitter & LinkedIn. We can use their APIs to collect user’s data automatically without bothering our visitors. In this tutorial, we are focusing on LinkedIn API using PHP to get user’s profile data.
We are going to use LinkedIn Profile API to fetch user’s data from their LinkedIn Account. I recommend you to read all mentioned steps below carefully.
Follow these steps to get user’s profile data with LinkedIn API using PHP
Step 1: Creating Application at LinkedIn’s developer console and setting things up.
Step 1.1: Sign in with your LinkedIn account.
Step 1.2: Visit LinkedIn Developer’s page.
Step 1.3: Click on “Create Application” button.
you have to fill all the mandatory fields of the application form.
after filling the form up you will get redirected to the application’s dashboard page (something like bellow one)
now copy the Client ID
& Client Secret
credentials and store it, we will use it further.
Step 1.4 – now you have to check (Checkbox) r_emailaddress
option in Default Application Permissions section and update your application by clicking on the update button.
Step 1.5 – now you have to add the
Step 2: this step contains the actual functionality of API, now we are going to serve a button “Register With LinkedIn” in front of users. When users click on that button they would get redirected to the LinkedIn page, this page asks the user to approve our application, after approval our application can access user’s personal data.
after clicking on “Allow” button users get redirected back to the URL mentioned in Step 1.5:, while this redirection LinkedIn sends some JSON encoded data, this data can be captured easily and can be used as anyway as you want.
Register with LinkedIn Button
<a href="https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id={CLIENT_ID}&redirect_uri={REDIRECT_URL}&state=987654321&scope=r_basicprofile">Register With LinkedIn</a>
you have to modify button code as per yours.
- just replace {CLIENT_ID} with your client_id (see Step 1.3 last line)
- and {REDIRECT_URL} with redirect URL you provided (see Step 1.5)
Step 3: now its time to get the user’s data, suppose I’ve added the redirect URL as http://domain.com/callback.php
(in Step 1.5), so callback.php
is the file where all the JSON data would be redirected, you can grab user’s data from this file. In below line, I’m going to show how you can capture the redirected JSON Data.
code for the callback.php
file is as follow. This is the file whose location (absolute URL of callback.php file) you have added in Step 1.5
if(isset($_GET['code'])){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.linkedin.com/oauth/v2/accessToken");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=authorization_code&code=".$_GET['code']."&redirect_uri={REDIRECT_URL}&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
}
if(isset($_GET['code']) && json_decode($server_output)->access_token != ''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.linkedin.com/v1/people/~?oauth2_access_token=".json_decode($server_output)->access_token."&format=json");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output2 = curl_exec ($ch);
curl_close ($ch);
$user_data = json_decode($server_output2);
print_r($user_data);
}
so all the user’s data finally get stored in the $user_data
variable. You can now store these details in a database as well.
If you find this tutorial helpful please don’t forget to share it with your friends because sharing is a caring!
You may also like
Please help us by showing your reaction
Hi, i can’t manage to make it work im sorry to bother u. But i have an this error :
“Trying to get property of non-object in C:\wamp64\www\linkedin\callback.php on line 11″
I’m not really understanding it, again i’m sorry.
I just have a problem with this part of the code :
” json_decode($server_output)->access_token ”
Can u help me understand what’s wrong.
Ty for your time.
Try to run this code on live server.
Thanks a lot ! It works… I’m sorry for the trouble.
Your blog is great and your reactivy is awesome !
I hope i’ll see more good contents like this one.
Good look and thanks again !
Thank you for such appreciating response !
Thank you,It Works.But Not Able To Get The All Details from linkedin,name,lastname and company name only i’m getting.
it is because maybe you only have such details updated in your profile.
No,I have all The Details In My LinkedIn,(Like Experience,Education,Languages,Certifications etc..)
But I m Not Able To Get Those Details From My LinkedIn,Can You Please Help Me In That.
Thanks for your Reply.
Now I Got The Answer,Thank you paritosh
THANKS ALOT SIR, YOU SAVE SAVE MY JOB
you’re most welcome @PRASHANT
how to get email id of user
Hotw to use this code in local server??
Unfortunately you can’t use it on local servers. Because LinkedIn authenticates that callback url on where all the user’s data have to send.
how to post the data on the linkedin wall using
this credential
print_r($user_data);
please help
Hello @Harveer,
unfortunately! what you’re saying cannot be done using these credentials, we need to use another LInkedIn’s API to post in linkedin’s database.
not to worry about it we surely gonna post on “How to post on LinkedIn wall” soon.
Thank you.
Awesome code snippet!
I am trying to find a way to retrieve my OWN LinkedIn posts, so I can show them on my blog.
So I don’t want my visitors to need to authorize themselves (just me) and only show my own posts.
Any suggestions?
you should try scrapping methods to scrap posts through LinkedIn.
Hi Paritosh Pandey ,
Your code was working beautifully.But, i want to crawl jobs in LinkedIn profiles .Can you please help me to retrieve page content from https://www.linkedin.com/jobs/ . Any suggestion please help ??
Hello @Sakthi,
the module you’re looking for is called
Scrapper
maybe this Quora question could help you.Thank you
Hi Paritosh Pandey ,
the above provided example is working fine on my end as well. please help me to retrieve the “Recommended Jobs” https://developer.linkedin.com/docs/guide/v2/jobs/recommended-jobs
as well.
Hello @Anoop,
after getting access token instead of
https://api.linkedin.com/v1/people/~?oauth2_access_token=".json_decode($server_output)->access_token."&format=json
use
https://api.linkedin.com/v2/recommendedJobs/~?oauth2_access_token=".json_decode($server_output)->access_token."&q=member&format=json
please let us know whether it is working or not.
Hello thanks for your appreciate information my question is that api free, I can use it without be afraid to expire my access?
Yes LinkedIn’s APIs are free under some circumstances.
Hi, I am not getting any data in callback page.
Thanks a lot for writing this useful piece of information as it helps marketers utilize the platform of Linkedin to engage and communicate with more number of people on a large scale.
hello sir,
i am facing some problem by using it.
Actually the problem is i am getting the error like given below:
Invalid HTTP Request
Could not process this client request HTTP method request for URL. Please double-check the URL (address) you used, or contact us if you feel you have reached this page in error.
Can you help me out in this.
By the way your blog is awesome…
please share code snippet you used, it’d help me to understand better.
Hi everyone! Is there any probabilities of Linkedin ban my account after try this? I need get fullprofile fields (position and education specifally) and I think that this will a good tool to extract information. Thanks!!
Hello Cristóbal,
using above tutorial is absolutely safe because this api is available globally by LinkedIn itself.
you can check here https://www.linkedin.com/developer/apps
Regards,
Hi, is it possible to extract data from basic profiles of those who we are not connected with?
hi.i use this code for my laravel project.but i got error.i want to know that what is this $_GET[‘code’] contain?i mean when i try to print it,it gives error like undefind variable.please help me out with this.
server_output2 is not getting any value on print_r($server_output2) its returning HTML with page not found
[…] Get User’s Profile With Linkedin API Using PHP – My … […]
Such great website
Amazing blog thanks for sharing today on this blog
issue in get the information – now working
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,”https://api.linkedin.com/v2/people/~?oauth2_access_token=”.$access_token.”&format=json”);
curl_setopt($ch1, CURLOPT_POST, 0);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
$server_output2 = curl_exec ($ch1);
curl_close ($ch1);