phillipsdata/linkedin

2.2.0 2019-03-04 16:27 UTC

README

This library allows you to communicate with the LinkedIn APIs. It is very generic and depends on the user to know their target endpoints and the data to be submitted.

Installation

Install via composer:

composer require phillipsdata/linkedin

Usage

To start, instantiate the class

$linkedin = new LinkedIn(
    'LINKEDIN_API_KEY',
    'LINKEDIN_API_SECRET',
    'LINKEDIN_CALLBACK_URL'
);

Get a URL to visit and be granted permissions from

$permissions_url = $linkedin->getPermissionUrl(
    array(
        'r_basicprofile',
        'r_liteprofile',
        'w_share',
        'w_member_social'
    )
);

The parameter here is a list of 'scopes'. If granted, they determine which API calls you are authorized to make.

API v1 Permissions

  • r_basicprofile
  • r_emailaddress
  • w_share
  • rw_company_admin

API v2 Permissions

  • r_liteprofile (replaces r_basicprofile)
  • r_emailaddress
  • w_member_social (replaces w_share)

After visiting the $permission_url, you will be redirected to the 'LINKEDIN_CALLBACK_URL' you submitted to the constructor. The redirect will submit a 'code' get parameter to that location which can be used to generate an access token that will be used to grant permission for future API calls.

$tokenResponse = $linkedin->getAccessToken($_GET['code']);

if ($tokenResponse->status() == 200) {
  // Record $tokenResponse->response() in some way
} else {
  echo $tokenResponse->errors();
}

This will return the access token if you want to store it somehow. Additionally it will set the token on your current LinkedIn object which will use it for any API calls you make.

After this you can make any api call you like as long you know the endpoint and data required

See the docs here for a full description of making a share request

$data = [
    'author' => 'urn:li:person:123456',
    'lifecycleState' => 'PUBLISHED',
    'specificContent' => [
        'com.linkedin.ugc.ShareContent' => [
            'shareCommentary' => [
                'text' => "Leverage LinkedIn's APIs to maximize engagement"
            ],
            'shareMediaCategory' => 'NONE'
        ]
    ],
    'visibility' => ['com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC']
];

$shareResponse = $linkedin->post('v2/ugcPosts', $data);

The response is returned as an LinkedInAPIResponse object that can be accessed like this

$shareResponse->headers(); // An array of header fields and their values
$shareResponse->raw(); // Exactly what was returned by LinkedIn, including headers
$shareResponse->response(); // An object containing the data returned by LinkedIn
$shareResponse->errors(); // Any errors given in the response
$shareResponse->status(); // The status code returned by the request

The API also has a method called share() which takes a bit of work off of the user by formatting the data how LinkedIn expects.

This method worked different pre v2.x. Instead it used version 1 of the LinkedIn api and simply defaulted the endpoint.

$shareResponse = $linkedin->share('Leverage LinkedIn's APIs to maximize engagement');

or

$shareResponse = $linkedin->share(
    'Leverage LinkedIn's APIs to maximize engagement',
    [
        'urn' => 'urn:li:digitalmediaAsset:C5422AQEbc381YmIuvg',
        'type' => 'IMAGE',
        'title' => 'LinkedIn API',
        'description' => 'Shows how great the LinkedIn API is.'
    ],
    'CONNECTIONS'
);