PHP code example of artisen2021 / linkedinsdk

1. Go to this page and download the library: Download artisen2021/linkedinsdk 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/ */

    

artisen2021 / linkedinsdk example snippets


$client = new Client(
'YOUR_LINKEDIN_APP_CLIENT_ID',
'YOUR_LINKEDIN_APP_CLIENT_SECRET'
);

$this->linkedInClient->setRedirectUrl('https://your.domain/callback');

$loginUrl = $client->getLoginUrl($scopes); 

$this->token = (new AccessTokenRequest($this->linkedInClient))->getAccessToken($code)->getToken();

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

$header = ['Authorization' => 'Bearer ' . $this->token];

$campaignGroupRequest = new CampaignGroupRequest($this->linkedInClient, $this->accessTokenCode);
$campaignGroup =  $campaignGroupRequest->create([
     'account_id' => env('LINKEDIN_ACCOUNT_ID'),
     'name' => 'CampaignResources Group Test',
     'start' => 1672531200000,
     'end' => 1672617600000,
     'total_budget' => '100',
     'currency' => 'EUR'
]);

$campaignGroupRequest = new CampaignGroupRequest($this->linkedInClient, $this->accessTokenCode);
$campaignGroupRequest->delete($campaignGroupId);

$campaignGroupRequest = new CampaignGroupRequest($this->linkedInClient, $this->accessTokenCode);
$campaignGroupRequest->update($campaignGroupId, $this->newParams);

$campaignRequest = new CampaignRequest($this->linkedInClient, $this->accessTokenCode);
$campaign =  $campaignRequest->create([
      'account_id' => env('LINKEDIN_ACCOUNT_ID'),
      'campaign_group_id' => $campaignGroupId,
      'dailyBudget' => [
          'amount' => '20',
          'currencyCode' => 'EUR',
      ],
      'costType' => 'CPM',
      'country' => 'NL',
      'language' => 'nl',
      'name' => 'CampaignResources Test',
      'start' => 1672531200000,
      'end' => 1672617600000,
      'locations' => [
          "urn:li:geo:103644278"
      ],
      'type' => 'SPONSORED_UPDATES',
      'unitCost' => [
          'amount' => '30',
          'currencyCode' => 'EUR',
      ],
      'status' => 'ACTIVE'
]);

$campaignRequest = new CampaignRequest($this->linkedInClient, $this->accessTokenCode);
$campaignRequest->delete($campaignId);

$campaignRequest = new CampaignRequest($this->linkedInClient, $this->accessTokenCode);
$campaignRequest->update($campaignId, $this->newParams);

        $adRequest = new AdRequest($this->linkedInClient, $this->accessTokenCode);
        $imageAd =  $adRequest->create([
            'account_id' => env('LINKEDIN_ACCOUNT_ID'),
            'linkedin_page_id' => env('LINKEDIN_PAGE_ID'),
            'campaign_id' => $campaignId,
            'media_type' => 'image',
            'message' => 'Image AdResources Test',
            'headline' => 'Image AdResources Test',
            'landing_page_url' => 'https://www.example.com/',
            'media_url' => 'https://www.example.com/image.jpg',
            'call_to_action' => 'attend',
        ]);

        $adRequest = new AdRequest($this->linkedInClient, $this->accessTokenCode);
        $adRequest->delete($adId);

        $adRequest = new AdRequest($this->linkedInClient, $this->accessTokenCode);
        $adRequest->update($adId, [
            'account_id' => env('LINKEDIN_ACCOUNT_ID'),
            'linkedin_page_id' => env('LINKEDIN_PAGE_ID'),
            'campaign_id' => $campaignId,
            'media_type' => 'image',
            'message' => 'Image AdResources Updated Test',
            'headline' => 'Image AdResources Updated Test',
            'landing_page_url' => 'https://www.example.com/',
            'media_url' => 'https://www.example2.com/image.jpg',
            'call_to_action' => 'attend'
        ]);

$targetingRequest = new TargetingRequest($this->linkedInClient, $this->accessTokenCode);
$adFetched = $targetingRequest->fetchLocation($location);

$targetingRequest = new TargetingRequest($this->linkedInClient, $this->accessTokenCode);
$urns = $targetingRequest->fetchUrns($query,$facet);

$targetingRequest = new TargetingRequest($this->linkedInClient, $this->accessTokenCode);
$similars = $targetingRequest->fetchSimilar($urn,$facet);

        $pendingPagesEvent = new LinkedInRequestPendingPagesEvent(env('LINKEDIN_PAGE_ID'));
        $socialPages = new SocialPageRequest($this->linkedInClient, $this->accessTokenCode);
        $pendingPages = $socialPages->getPendingClientPages($pendingPagesEvent);

        $pageDataEvent = new LinkedInGetPageDataEvent(env('LINKEDIN_PAGE_ID'));
        $socialPages = new SocialPageRequest($this->linkedInClient, $this->accessTokenCode);
        $pageData = $socialPages->getPageData($pageDataEvent);

        $pageCurrentStatusEvent = new LinkedInGetPageCurrentStatusEvent(env('LINKEDIN_PAGE_ID'));
        $socialPages = new SocialPageRequest($this->linkedInClient, $this->accessTokenCode);
        $pageCurrentStatus = $socialPages->getPageCurrentStatus($pageCurrentStatusEvent);

        $adAccountsEvent = new LinkedInRequestAdAccountsEvent(env('LINKEDIN_PAGE_ID'));
        $socialPages = new SocialPageRequest($this->linkedInClient, $this->accessTokenCode);
        $adAccounts = $socialPages->searchAdAccountsByPageId($adAccountsEvent);