PHP code example of konnektor-app / linkedin-api-php-client
1. Go to this page and download the library: Download konnektor-app/linkedin-api-php-client 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' );
konnektor-app / linkedin-api-php-client example snippets
$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 ;
$scopes = [
Scope::READ_LITE_PROFILE,
Scope::READ_EMAIL_ADDRESS,
Scope::SHARE_AS_USER,
Scope::SHARE_AS_ORGANIZATION,
];
$loginUrl = $client->getLoginUrl($scopes);
$accessToken = $client->getAccessToken($_GET['code' ]);
file_put_contents('token.json' , json_encode($accessToken));
use LinkedIn \AccessToken ;
use LinkedIn \Client ;
$client = new Client(
'LINKEDIN_APP_CLIENT_ID' ,
'LINKEDIN_APP_CLIENT_SECRET'
);
$tokenString = file_get_contents('token.json' );
$tokenData = json_decode($tokenString, true );
$accessToken = new AccessToken($tokenData['token' ], $tokenData['expiresAt' ]);
$client->setAccessToken($accessToken);
$profile = $client->api(
'ENDPOINT' ,
['parameter name' => 'its value here' ],
'HTTP method like GET for example'
);
$client->get('ENDPOINT' , ['param' => 'value' ]);
$client->post('ENDPOINT' , ['param' => 'value' ]);
$client->delete('ENDPOINT' );
$profile = $client->get(
'me' ,
['fields' => 'id,firstName,lastName' ]
);
print_r($profile);
$profile = $client->get(
'organizations' ,
['is-company-admin' => true ]
);
print_r($profile);
$share = $client->post(
'ugcPosts' ,
[
'author' => 'urn:li:person:' . $profile['id' ],
'lifecycleState' => 'PUBLISHED' ,
'specificContent' => [
'com.linkedin.ugc.ShareContent' => [
'shareCommentary' => [
'text' => 'Checkout this amazing PHP SDK for LinkedIn!'
],
'shareMediaCategory' => 'ARTICLE' ,
'media' => [
[
'status' => 'READY' ,
'description' => [
'text' => 'OAuth 2 flow, composer Package.'
],
'originalUrl' => 'https://github.com/zoonman/linkedin-api-php-client' ,
'title' => [
'text' => 'PHP Client for LinkedIn API'
]
]
]
]
],
'visibility' => [
'com.linkedin.ugc.MemberNetworkVisibility' => 'CONNECTIONS'
]
]
);
print_r($share);
$companyId = '123' ;
$companyInfo = $client->get('organizations/' . $companyId);
print_r($companyInfo);
$companyId = '2414183' ;
$share = $client->post(
'ugcPosts' ,
[
'author' => 'urn:li:organization:' . $companyId,
'lifecycleState' => 'PUBLISHED' ,
'specificContent' => [
'com.linkedin.ugc.ShareContent' => [
'shareCommentary' => [
'text' => 'Checkout this amazing PHP SDK for LinkedIn!'
],
'shareMediaCategory' => 'ARTICLE' ,
'media' => [
[
'status' => 'READY' ,
'description' => [
'text' => 'OAuth 2 flow, composer Package.'
],
'originalUrl' => 'https://github.com/zoonman/linkedin-api-php-client' ,
'title' => [
'text' => 'PHP Client for LinkedIn API'
]
]
]
]
],
'visibility' => [
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
]
]
);
print_r($share);
$client->setApiHeaders([
'Content-Type' => 'application/json' ,
'x-li-format' => 'json' ,
'X-Restli-Protocol-Version' => '2.0.0' ,
'x-li-src' => 'msdk'
]);
$client->setApiRoot('https://api.linkedin.com/v2/' );
$filename = '/path/to/image.jpg' ;
$client->setApiRoot('https://api.linkedin.com/' );
$mp = $client->upload($filename);