globules-io/linkedin-php-sdk
Composer 安装命令:
composer require globules-io/linkedin-php-sdk
包简介
This package is an SDK for using LinkedIn V2 Marketing API. You can use it for managing Company Pages in this social network
关键字:
README 文档
README
Installation
You will need at least PHP 7.4. Compatible with PHP 8.x
Use composer package manager to install the lastest version of the package:
composer require globules-io/linkedin-php-sdk
Or add this package as dependency to composer.json.
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. Once your app has been approved, you will get a ClientId and ClientSecret, that you will use later.
LinkedIn Restrictions
The scopes or permissions aren't available right away when you register an application with LinkedIn.
When you first create your app in the developer portal, you only have access to the w_member_social scope.
You can start developing but the token you obtain at this stage will have no refresh information.
Then you need to request access to the Marketing Developer Platform which will give you 2 additionals scopes, r_emailaddress, r_liteprofile
Once your request has been approved, you will get access to all the remaining scopes and your token will have refresh information.
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.
You can read more about Linkedin Api scopes here.
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);
Renewing Tokens
LinkedIn tokens expire after 60 days but you can renew your access token by using the refresh token. Tokens can be then refreshed for 365 days, after which the end-user must re-auth.
$client->renewTokenFromRefreshToken($refreshToken);
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( 'organizationalEntityAcls', ['q' => 'roleAssignee'] ); print_r($profile);
Get Company page profile
$companyId = '123'; // use id of the company where you are an admin $companyInfo = $client->get('organizations/' . $companyId); print_r($companyInfo);
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/');
globules-io/linkedin-php-sdk 适用场景与选型建议
globules-io/linkedin-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 311 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「rest」 「api」 「Authentication」 「client」 「oauth」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 globules-io/linkedin-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 globules-io/linkedin-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 globules-io/linkedin-php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Laravel JWT auth service package
Zoho OAuth 2.0 Client Provider for The PHP League OAuth2-Client
统计信息
- 总下载量: 311
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-01