symfgenus / linkedin-api-php-client
支持OAuth 2.0和CSRF的LinkedIn API PHP SDK。可用于社交登录。
Requires
- php: >=7.0
- ext-curl: *
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: @stable
- vlucas/phpdotenv: ~2.0
README
注意!
这是一个从Zoonman原始代码的分支。
基于PHP编写的具有OAuth 2授权的LinkedIn API客户端
安装
您至少需要PHP 5.6。PHP 5.5已弃用超过一年!现在是升级的时候了。
使用composer包管理器安装最新版本的包
composer require zoonman/linkedin-api-php-client
或将此包作为依赖项添加到composer.json
。
如果您从未使用过Composer,请从这里开始安装Composer。
使用方法
要开始使用LinkedIn API,您需要获取应用程序客户端ID和密钥。
访问LinkedIn开发者门户,在“我的应用”部分创建新应用。
引导自动加载和实例化客户端
// ... 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。您可以设置自己的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_BASIC_PROFILE, Scope::READ_EMAIL_ADDRESS, Scope::MANAGE_COMPANY, Scope::SHARING, ]; $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));
由于安全考虑,不建议以这种方式存储令牌,并且仅用于演示目的。请确保以安全的方式存储令牌。
设置访问令牌
您可以使用LinkedIn\Client
类的setAccessToken()
方法设置存储为字符串的令牌。您必须将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( 'people/~:(id,email-address,first-name,last-name)' ); print_r($profile);
列出您是管理员的公司
$profile = $client->get( 'companies', ['is-company-admin' => true] ); print_r($profile);
在个人配置文件上分享内容
确保图像URL可以从互联网上获取(不要在图像URL中使用localhost)。
$share = $client->post( 'people/~/shares', [ 'comment' => 'Checkout this amazing PHP SDK for LinkedIn!', 'content' => [ 'title' => 'PHP Client for LinkedIn API', 'description' => 'OAuth 2 flow, composer Package', 'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client', 'submitted-image-url' => 'https://github.com/fluidicon.png', ], 'visibility' => [ 'code' => 'anyone' ] ] ); print_r($share);
获取公司页面配置文件
$companyId = '123'; // use id of the company where you are an admin $companyInfo = $client->get('companies/' . $companyId . ':(id,name,num-followers,description)'); 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( 'companies/' . $companyId . '/shares', [ 'comment' => 'Checkout this amazing PHP SDK for LinkedIn!', 'content' => [ 'title' => 'PHP Client for LinkedIn API', 'description' => 'OAuth 2 flow, composer Package', 'submitted-url' => 'https://github.com/zoonman/linkedin-api-php-client', 'submitted-image-url' => 'https://github.com/fluidicon.png', ], 'visibility' => [ 'code' => 'anyone' ] ] ); 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/');
图片上传
我假设你必须成为LinkedIn合作伙伴或类似的人。
尝试上传图片到LinkedIn。参见富媒体分享(对我来说返回“没有足够的权限访问媒体资源”)。
$filename = '/path/to/image.jpg'; $client->setApiRoot('https://api.linkedin.com/'); $mp = $client->upload($filename);
贡献
请,使用GitHub问题关联你的更改打开PR。你的代码必须遵循PSR标准,并包含PHPUnit测试。