michelmelo / linkedin-api-php-client
LinkedIn API PHP SDK,支持OAuth 2.0和CSRF。可用于LinkedIn社交登录或分享。示例。文档。
Requires
- php: ^7.4 || ^8.0
- ext-curl: *
- guzzlehttp/guzzle: ^7.3.0
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: @stable
- vlucas/phpdotenv: ~2.0
This package is auto-updated.
Last update: 2024-09-05 13:42:14 UTC
README
安装
您至少需要PHP 7.3。我们支持官方支持的PHP版本。
使用composer包管理器安装包的最新版本
composer require michelmelo/linkedin-api-php-client
或者将此包添加为composer.json
中的依赖项。
如果您从未使用过Composer,请从这里开始,并安装composer。
开始使用
在您开始之前,请访问LinkedIn API 文档。这将节省您大量时间并避免一些愚蠢的问题。
要开始使用LinkedIn API,您需要获取应用程序客户端ID和密钥。
访问LinkedIn开发者门户并在“我的应用”部分创建新应用。保存ClientID和ClientSecret,您稍后会使用它们。
引导自动加载器和实例化客户端
// ... 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' );
获取本地重定向URL
要开始链接过程,您必须设置重定向URL。您可以使用自己的或使用当前的一个。SDK为您提供了一个方便的getRedirectUrl()
辅助方法。
$redirectUrl = $client->getRedirectUrl();
我们建议您在链接会话期间保存它,因为您在获取访问令牌时将需要使用它。
设置本地重定向URL
设置自定义重定向URL
$client->setRedirectUrl('http://your.domain.tld/path/to/script/');
获取LinkedIn重定向URL
为了执行OAUTH 2.0流程,您需要获取LinkedIn登录URL。在此过程中,您必须定义请求的权限范围。使用Scope
枚举类来获取范围名称。要获取LinkedIn的重定向URL,请使用以下方法
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
现在您可以带用户去LinkedIn了。您可以使用链接或依赖Location HTTP头。
获取访问令牌
要获取访问令牌(不要忘记设置重定向URL)
$accessToken = $client->getAccessToken($_GET['code']);
此方法返回LinkedIn\AccessToken
类的对象。您可以像这样将此令牌存储在文件中
file_put_contents('token.json', json_encode($accessToken));
由于安全考虑,不建议以这种方式存储令牌,仅用于演示目的。请确保安全地存储令牌。
设置访问令牌
您可以使用setAccessToken()
方法为LinkedIn\Client
类设置存储为字符串的令牌。您必须传递LinkedIn\AccessToken
实例到该方法。
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);
执行API调用
所有API调用都可以通过简单的方法调用
$profile = $client->api( 'ENDPOINT', ['parameter name' => 'its value here'], 'HTTP method like GET for example' );
有3个辅助方法
// get method $client->get('ENDPOINT', ['param' => 'value']); //post $client->post('ENDPOINT', ['param' => 'value']); // delete $client->delete('ENDPOINT');
示例
执行API调用以获取配置文件信息
$profile = $client->get( 'me', ['fields' => 'id,firstName,lastName'] ); print_r($profile);
列出您是管理员的公司
$profile = $client->get( 'organizations', ['is-company-admin' => true] ); print_r($profile);
在个人配置文件上分享内容
确保图片URL可以从互联网访问(不要在图片URL中使用localhost)。
$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);
获取公司页面配置文件
$companyId = '123'; // use id of the company where you are an admin $companyInfo = $client->get('organizations/' . $companyId); print_r($companyInfo);
在LinkedIn企业页面上分享内容
// 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);
设置自定义API请求头
更改发送给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 ]);
更改默认API根目录
一些私有API访问。
$client->setApiRoot('https://api.linkedin.com/v2/');
图片上传
我假设您必须是领英合作伙伴或类似身份。
尝试将图片上传到领英。请参阅富媒体分享
- https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin(对我来说返回“访问媒体资源权限不足”)。
$filename = '/path/to/image.jpg'; $client->setApiRoot('https://api.linkedin.com/'); $mp = $client->upload($filename);
贡献
请打开PR(Pull Request,即拉取请求),将您的更改链接到GitHub问题。您的代码必须遵循PSR标准,并包含PHPUnit测试。