PHP code example of avansaber / php-linkedin-api

1. Go to this page and download the library: Download avansaber/php-linkedin-api 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/ */

    

avansaber / php-linkedin-api example snippets


use Avansaber\LinkedInApi\Auth\Auth;
use Avansaber\LinkedInApi\Auth\Pkce;
use Avansaber\LinkedInApi\Auth\Scope;

$auth = new Auth();
$state = bin2hex(random_bytes(16));
$authUrl = $auth->getAuthUrl(
    clientId: 'your-client-id',
    redirectUri: 'https://your-app/callback',
    scopes: [Scope::R_LITEPROFILE->value, Scope::W_MEMBER_SOCIAL->value],
    pkce: null, // or new Pkce($codeVerifier, $codeChallenge)
    state: $state
);
// Redirect user to $authUrl

// On callback, exchange the code for a token (perform the HTTP request using your HTTP client)
$params = $auth->getAccessToken('client-id', 'client-secret', $_GET['code'], 'https://your-app/callback', null);
// $params['endpoint'] and $params['params'] contain the token endpoint and form params

use Avansaber\LinkedInApi\Http\ClientConfig;
use Avansaber\LinkedInApi\Http\LinkedInApiClient;
use Nyholm\Psr7\Factory\Psr17Factory;
use Http\Client\Curl\Client as CurlClient; // any PSR-18 client

$psr17 = new Psr17Factory();
$http = new CurlClient();
$config = new ClientConfig(linkedInVersion: '202401');
$client = new LinkedInApiClient($http, $psr17, $psr17, $config, 'ACCESS_TOKEN');

use Avansaber\LinkedInApi\Resources\Me;
$me = new Me($client);
$profile = $me->get(); // tries /rest/me then falls back to /v2/me

use Avansaber\LinkedInApi\Resources\Organizations;
$orgs = new Organizations($client);
$org = $orgs->get(123456);

use Avansaber\LinkedInApi\Resources\UgcPosts;
use Avansaber\LinkedInApi\Data\Requests\PostCreateRequest;
$ugc = new UgcPosts($client);
$resp = $ugc->create(new PostCreateRequest('urn:li:organization:123', 'Hello World'));

use Avansaber\LinkedInApi\Resources\Posts;
$posts = new Posts($client);
$post = $posts->get('urn:li:ugcPost:abc');

use Avansaber\LinkedInApi\Resources\SocialActions;
$sa = new SocialActions($client);
$comments = $sa->comments('urn:li:ugcPost:abc');
$likes = $sa->likes('urn:li:ugcPost:abc');

use Avansaber\LinkedInApi\Http\PaginatorIterator;
$iter = PaginatorIterator::iterate(function(int $start, int $count) use ($client) {
    return $client->get('organizations?q=vanityName&vanityName=linkedin&start='.$start.'&count='.$count, [], false);
}, 10);
foreach ($iter as $row) { /* ... */ }

use Avansaber\LinkedInApi\Media\MediaUploadHelper;
$helper = new MediaUploadHelper($client, $psr17);
$init = $helper->initializeImageUpload('urn:li:organization:123');
$uploadUrl = $init['value']['uploadUrl'];
$helper->uploadBinary($uploadUrl, file_get_contents('/path/image.jpg'));

use Avansaber\LinkedInApi\Auth\Scope;

// Modern OpenID Connect scopes (recommended)
$scopes = [
    Scope::OPENID->value,    // Use your name and photo
    Scope::PROFILE->value,   // Use your name and photo  
    Scope::EMAIL->value,     // Use the primary email address
];

// Or use the helper method
$scopes = Scope::getModernProfileScopes();

use Avansaber\LinkedInApi\Resources\Me;

$me = new Me($client);

// Modern approach - uses OpenID Connect userinfo endpoint
$profile = $me->getUserInfo(); // Recommended

// Response format (OpenID Connect standard):
// {
//   "sub": "linkedin-user-id",
//   "name": "John Doe", 
//   "given_name": "John",
//   "family_name": "Doe",
//   "email": "[email protected]",
//   "picture": "https://..."
// }
bash
composer