LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
You will need at least PHP 5.6. PHP 5.5 was deprecated more than a year ago! This is the time for upgrade.
Use composer package manager to install the lastest version of the package:
composer require zoonman/linkedin-api-php-client
Or add this package as dependency to
If you have never used Composer, you should start here and install composer.
To start working with LinkedIn API, you will need to get application client id and secret.
Go to LinkedIn Developers portal and create new application in section My Apps.
// ... please, add composer autoloader first include_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; // import client class use LinkedIn\Client; // instantiate the Linkedin client $client = new Client( 'YOUR_LINKEDIN_APP_CLIENT_ID', 'YOUR_LINKEDIN_APP_CLIENT_SECRET' );
To start linking process you have to setup redirect url.
You can set your own or use current one.
SDK provides you a
getRedirectUrl() helper for your convenience:
$redirectUrl = $client->getRedirectUrl();
We recommend you to have it stored during the linking session because you will need to use it when you will be getting access token.
Set a custom redirect url use:
In order of performing OAUTH 2.0 flow, you should get LinkedIn login URL.
During this procedure you have to define scope of requested permissions.
Scope enum class to get scope names.
To get redirect url to LinkedIn, use the following approach:
use LinkedIn\Scope; // define scope $scopes = [ Scope::READ_BASIC_PROFILE, Scope::READ_EMAIL_ADDRESS, Scope::MANAGE_COMPANY, Scope::SHARING, ]; $loginUrl = $client->getLoginUrl($scopes); // get url on LinkedIn to start linking
Now you can take user to LinkedIn. You can use link or rely on Location HTTP header.
To get access token use (don't forget to set redirect url)
$accessToken = $client->getAccessToken($_GET['code']);
This method returns object of
You can store this token in the file like this:
This way of storing tokens is not recommended due to security concerns and used for demonstration purpose. Please, ensure that tokens are stored securely.
You can use method
setAccessToken() for the
LinkedIn\Client class to set token stored as string. You have to pass
LinkedIn\AccessToken to this method.
use LinkedIn\AccessToken; use LinkedIn\Client; // instantiate the Linkedin client $client = new Client( 'LINKEDIN_APP_CLIENT_ID', 'LINKEDIN_APP_CLIENT_SECRET' ); // load token from the file $tokenString = file_get_contents('token.json'); $tokenData = json_decode($tokenString, true); // instantiate access token object from stored data $accessToken = new AccessToken($tokenData['token'], $tokenData['expiresAt']); // set token for client $client->setAccessToken($accessToken);
All API calls can be called through simple method:
$profile = $client->api( 'ENDPOINT', ['parameter name' => 'its value here'], 'HTTP method like GET for example' );
There are 3 helper methods:
// get method $client->get('ENDPOINT', ['param' => 'value']); //post $client->post('ENDPOINT', ['param' => 'value']); // delete $client->delete('ENDPOINT');
$profile = $client->get( 'people/~:(id,email-address,first-name,last-name)' ); print_r($profile);
$profile = $client->get( 'companies', ['is-company-admin' => true] ); print_r($profile);
Make sure that image URL is available from the Internet (don't use localhost in the image url).
$share = $client->post( 'people/~/shares', [ 'comment' => 'Checkout this amazing PHP SDK for LinkedIn!', 'content' => [ 'title' => 'PHP Client for LinkedIn API', 'description' => 'OAuth 2 flow, composer Package', 'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client', 'submitted-image-url' => 'https://github.com/fluidicon.png', ], 'visibility' => [ 'code' => 'anyone' ] ] ); print_r($share);
$companyId = '123'; // use id of the company where you are an admin $companyInfo = $client->get('companies/' . $companyId . ':(id,name,num-followers,description)'); print_r($companyInfo);
// set sandboxed company page to work with // you can check updates at // https://www.linkedin.com/company/devtestco $companyId = '2414183'; $share = $client->post( 'companies/' . $companyId . '/shares', [ 'comment' => 'Checkout this amazing PHP SDK for LinkedIn!', 'content' => [ 'title' => 'PHP Client for LinkedIn API', 'description' => 'OAuth 2 flow, composer Package', 'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client', 'submitted-image-url' => 'https://github.com/fluidicon.png', ], 'visibility' => [ 'code' => 'anyone' ] ] ); print_r($share);
Change different headers sent to LinkedIn API.
$client->setApiHeaders([ 'Content-Type' => 'application/json', 'x-li-format' => 'json', 'X-Restli-Protocol-Version' => '2.0.0', // use protocol v2 'x-li-src' => 'msdk' // set a src header to "msdk" to mimic a mobile SDK ]);
Some private API access there.
I assume you have to be LinkedIn partner or something like that.
Try to upload image to LinkedIn. See Rich Media Shares (returns "Not enough permissions to access media resource" for me).
$filename = '/path/to/image.jpg'; $client->setApiRoot('https://api.linkedin.com/'); $mp = $client->upload($filename);
Please, open PR with your changes linked to an GitHub issue. You code must follow PSR standards and have PHPUnit tests.