All Projects → zoonman → Linkedin Api Php Client

zoonman / Linkedin Api Php Client

Licence: mit
LinkedIn API PHP SDK with OAuth 2 support. Can be used for social sign in or sharing on LinkedIn. Has a good usage examples

Projects that are alternatives of or similar to Linkedin Api Php Client

Mailchimp Api 3.0 Php
A feature rich object-oriented PHP library for interacting with MailChimp's API v3 💌🐵
Stars: ✭ 61 (-30.68%)
Mutual labels:  sdk, composer, oauth2
Auth0.js
Auth0 headless browser sdk
Stars: ✭ 755 (+757.95%)
Mutual labels:  sdk, oauth2
Openapi Sdk Php
Alibaba Cloud SDK for PHP
Stars: ✭ 423 (+380.68%)
Mutual labels:  sdk, composer
Zhima
芝麻信用商家服务PHP版SDK。目前为止,是github上最好用的php版SDK
Stars: ✭ 13 (-85.23%)
Mutual labels:  sdk, composer
Oauth
🔗 OAuth 2.0 implementation for various providers in one place.
Stars: ✭ 336 (+281.82%)
Mutual labels:  linkedin, oauth2
Oauth2
Go OAuth2
Stars: ✭ 3,941 (+4378.41%)
Mutual labels:  oauth2, oauth2-client
Oauth2
OAuth2 client in Go
Stars: ✭ 20 (-77.27%)
Mutual labels:  oauth2, oauth2-client
Perfect-Authentication
OAuth2 Implementations with Facebook, Google, LinkedIn, Slack, SalesForce and GitHub providers.
Stars: ✭ 14 (-84.09%)
Mutual labels:  oauth2, linkedin
Oauth2
OAuth2 framework for macOS and iOS, written in Swift.
Stars: ✭ 983 (+1017.05%)
Mutual labels:  oauth2, oauth2-client
Psraw
PowerShell Reddit API Wrapper
Stars: ✭ 42 (-52.27%)
Mutual labels:  oauth2, oauth2-client
Socialite
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Stars: ✭ 1,026 (+1065.91%)
Mutual labels:  linkedin, oauth2
Hiauth
HiAuth是一个开源的基于Oauth2协议的认证、授权系统。
Stars: ✭ 273 (+210.23%)
Mutual labels:  oauth2, oauth2-client
Oauthswift
Swift based OAuth library for iOS
Stars: ✭ 2,949 (+3251.14%)
Mutual labels:  oauth2, oauth2-client
Retroauth
A library build on top of retrofit, for simple handling of authenticated requests
Stars: ✭ 405 (+360.23%)
Mutual labels:  oauth2, oauth2-client
elm-oauth2
OAuth 2.0 client-side utils in Elm
Stars: ✭ 74 (-15.91%)
Mutual labels:  oauth2, oauth2-client
Cpprestsdk
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Stars: ✭ 6,631 (+7435.23%)
Mutual labels:  sdk, oauth2
oxd
Client software to secure apps with OAuth 2.0, OpenID Connect, and UMA
Stars: ✭ 40 (-54.55%)
Mutual labels:  oauth2, oauth2-client
SimpleOAuth
Simple OAuth 2.0 for Android
Stars: ✭ 15 (-82.95%)
Mutual labels:  oauth2, oauth2-client
React Native Linkedin Sdk
React Native Wrapper for Latest LinkedIn Mobile SDK for Sign-In / Auth and API Access.
Stars: ✭ 37 (-57.95%)
Mutual labels:  linkedin, oauth2
Psmsgraph
A PowerShell module for the Microsoft Graph API
Stars: ✭ 71 (-19.32%)
Mutual labels:  oauth2, oauth2-client

LinkedIn API Client with OAuth 2 authorization written on PHP

Build Status Code Climate Packagist GitHub license

See complete example inside index.php to get started.

Installation

You will need at least PHP 7.3. We match officially supported versions of PHP.

Use composer package manager to install the lastest version of the package:

composer require zoonman/linkedin-api-php-client

Or add this package as dependency to composer.json.

If you have never used Composer, you should start here and install composer.

Get Started

Before you will get started, play visit to LinkedIn API Documentation. This will save you a lot of time and prevent some silly questions.

To start working with LinkedIn API, you will need to get application client id and secret.

Go to LinkedIn Developers portal and create new application in section My Apps. Save ClientId and ClientSecret, you will use them later.

Bootstrapping autoloader and instantiating a client

// ... please, add composer autoloader first
include_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

// import client class
use LinkedIn\Client;

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

Getting local redirect URL

To start linking process you have to setup redirect url. You can set your own or use current one. SDK provides you a getRedirectUrl() helper for your convenience:

$redirectUrl = $client->getRedirectUrl();

We recommend you to have it stored during the linking session because you will need to use it when you will be getting access token.

Setting local redirect URL

Set a custom redirect url use:

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

Getting LinkedIn redirect URL

In order of performing OAUTH 2.0 flow, you should get LinkedIn login URL. During this procedure you have to define scope of requested permissions. Use Scope enum class to get scope names. To get redirect url to LinkedIn, use the following approach:

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

Now you can take user to LinkedIn. You can use link or rely on Location HTTP header.

Getting Access Token

To get access token use (don't forget to set redirect url)

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

This method returns object of LinkedIn\AccessToken class. You can store this token in the file like this:

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

This way of storing tokens is not recommended due to security concerns and used for demonstration purpose. Please, ensure that tokens are stored securely.

Setting Access Token

You can use method setAccessToken() for the LinkedIn\Client class to set token stored as string. You have to pass instance of LinkedIn\AccessToken to this method.

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

Performing API calls

All API calls can be called through simple method:

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

There are 3 helper methods:

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

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

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

Examples

Perform api call to get profile information
$profile = $client->get(
    'me',
    ['fields' => 'id,firstName,lastName']
);
print_r($profile);
List companies where you are an admin
$profile = $client->get(
    'organizations',
    ['is-company-admin' => true]
);
print_r($profile);
Share content on a personal profile

Make sure that image URL is available from the Internet (don't use localhost in the image url).

$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);
Get Company page profile
$companyId = '123'; // use id of the company where you are an admin
$companyInfo = $client->get('organizations/' . $companyId);
print_r($companyInfo);
Share content on a LinkedIn business page
// set sandboxed company page to work with
// you can check updates at
// https://www.linkedin.com/company/devtestco
$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);
Setup custom API request headers

Change different headers sent to LinkedIn API.

$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
]);
Change default API root

Some private API access there.

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

I assume you have to be LinkedIn partner or something like that.

Try to upload image to LinkedIn. See Rich Media Shares (returns "Not enough permissions to access media resource" for me).

$filename = '/path/to/image.jpg';
$client->setApiRoot('https://api.linkedin.com/');
$mp = $client->upload($filename);

Contributing

Please, open PR with your changes linked to an GitHub issue. You code must follow PSR standards and have PHPUnit tests.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].