PHP code example of samoritano / linkedin-api-php-client-v2

1. Go to this page and download the library: Download samoritano/linkedin-api-php-client-v2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

samoritano / linkedin-api-php-client-v2 example snippets


// ... please, add composer autoloader first


// instantiate the Linkedin client
$client = new Client(
    'YOUR_LINKEDIN_APP_CLIENT_ID',
    'YOUR_LINKEDIN_APP_CLIENT_SECRET'
);

$redirectUrl = $client->getRedirectUrl();

$client->setRedirectUrl('http://your.domain.tld/path/to/script/');

use LinkedIn\Scope;

// define scope
$scopes = [
  Scope::READ_LITE_PROFILE,
  Scope::READ_EMAIL_ADDRESS,
  Scope::SHARE_AS_USER,
  Scope::SHARE_AS_ORGANIZATION,
];
$loginUrl = $client->getLoginUrl($scopes); // get url on LinkedIn to start linking

$accessToken = $client->getAccessToken($_GET['code']);

file_put_contents('token.json', json_encode($accessToken));

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);

$profile = $client->api(
    'ENDPOINT',
    ['parameter name' => 'its value here'],
    'HTTP method like GET for example'
);

// get method
$client->get('ENDPOINT', ['param' => 'value']);

//post
$client->post('ENDPOINT', ['param' => 'value']);

// delete
$client->delete('ENDPOINT');

$profile = $client->get(
    'me',
    ['fields' => 'id,firstName,lastName']
);
print_r($profile);

$profile = $client->get(
    'organizationalEntityAcls',
    ['q' => 'roleAssignee']
);
print_r($profile);

$companyId = '123'; // use id of the company where you are an admin
$companyInfo = $client->get('organizations/' . $companyId);
print_r($companyInfo);

$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
]);

$client->setApiRoot('https://api.linkedin.com/v2/');