This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Linkedin API v2 integration for Laravel and Lumen 5

1 2018-06-19 13:17 UTC

This package is not auto-updated.

Last update: 2021-10-04 14:14:41 UTC


Linkedin API v2 integration for Laravel Framework


This package is a wrapper for Scottybo/LinkedIn-API-client-v2. You can view the documentation for php version here. Don't forget to consult the oficial LinkedIn API site.

If you need install on Lumen, go to Lumen section

Installation on Laravel

Install with composer
composer require scottybo/laravel-linkedin-v2
Add service Provider
'LinkedInV2'  => \Scottybo\LinkedIn\Facades\LinkedInV2::class,
Publish config file
php artisan vendor:publish --provider="Scottybo\LinkedIn\LinkedinServiceProviderV2"

Installation on Lumen

Install with composer
composer require scottybo/laravel-linkedin-v2
Add Service Provider, facade and config parameters to the bootstrap/app.php file and copy the linkedin.php to the config directory of your project (create then if not exists)



In order to use this API client (or any other LinkedIn clients) you have to register your app with LinkedIn to receive an API key. Once you've registered your LinkedIn app, you will be provided with an API Key and Secret Key, please fill this values on linkedin.php config file.

####Basic Usage The unique difference in this package is the LinkedInV2 facade. Instead of this:

$linkedIn=new ScottyBo\LinkedIn\LinkedIn('app_id', 'app_secret');

you can simple call the facade for anyone method, like this:


or use the laravel container:

App::make('linkedin-v2')->foo(); // ...

The service container automatically return an instance of LinkedInV2 class ready to use

LinkedIn login

This example below is showing how to login with LinkedIn using LinkedIn facade.

if (LinkedInV2::isAuthenticated()) {
     //we know that the user is authenticated now. Start query the API
     echo  "Welcome ".$user['firstName'];
}elseif (LinkedInV2::hasError()) {
     echo  "User canceled the login.";

//if not authenticated
$url = LinkedInV2::getLoginUrl();
echo "<a href='$url'>Login with LinkedIn</a>";

Get basic profile info

You can retrive information using the get() method, like this:


This query return an array of information. You can view all the REST api's methods in REST API Console

How to post on LinkedIn wall

The example below shows how you can post on a users wall. The access token is fetched from the database.


$options = ['json'=>
        'comment' => 'Im testing Scottybo LinkedIn v2 client with Laravel Framework!',
        'visibility' => [
               'code' => 'anyone'

$result = LinkedIn::post('v2/people/~/shares', $options);

You may of course do the same in xml. Use the following options array.

$options = array(
'format' => 'xml',
'body' => '<share>
 <comment>Im testing Scottybo LinkedIn v2 client with Laravel Framework!</comment>


The api options

The third parameter of LinkedInV2::api is an array with options. Below is a table of array keys that you may use.

Option name Description
body The body of a HTTP request. Put your xml string here.
format Set this to 'json', 'xml' or 'simple_xml' to override the default value.
headers This is HTTP headers to the request
json This is an array with json data that will be encoded to a json string. Using this option you do need to specify a format.
response_data_type To override the response format for one request
query This is an array with query parameters

Changing request format

The default format when communicating with LinkedIn API is json. You can let the API do json_encode for you. The following code shows you how.

$body = array(
    'comment' => 'Testing the linkedin v2 API!',
    'visibility' => array('code' => 'anyone')

LinkedInV2::post('v1/people/~/shares', array('json'=>$body));
LinkedInV2::post('v1/people/~/shares', array('body'=>json_encode($body)));

When using array('json'=>$body) as option the format will always be json. You can change the request format in three ways.

// By setter

// Set format for just one request
LinkedInV2::post('v1/people/~/shares', array('format'=>'xml', 'body'=>$body));

Understanding response data type

The data type returned from LinkedIn::api can be configured. You may use the LinkedInV2::setResponseDataType or as an option for LinkedInV2::api

// By setter

// Set format for just one request
LinkedInV2::get('v2/me', array('response_data_type'=>'psr7'));

Below is a table that specifies what the possible return data types are when you call LinkedIn::api.

Type Description
array An assosiative array. This can only be used with the json format.
simple_xml A SimpleXMLElement. See PHP manual. This can only be used with the xml format.
psr7 A PSR7 response.
stream A file stream.
string A plain old string.

Using different scopes

If you want to define special scopes when you authenticate the user you should specify them when you are generating the login url. If you don't specify scopes LinkedIn will use the default scopes that you have configured for the app.

$scope = 'r_fullprofile,r_emailaddress,w_share';
$scope = array('rw_groups', 'r_contactinfo', 'r_fullprofile', 'w_messages');

$url = LinkedInV2::getLoginUrl(array('scope'=>$scope));
return "<a href='$url'>Login with LinkedIn</a>";


You can view the latest changes here