sergiomaximiliano / linkedin-api-php-client
LinkedIn API PHP SDK,支持OAuth 2.0与CSRF保护。可用于社交登录或LinkedIn分享。示例。文档。
Requires
- php: >=5.6
- ext-curl: *
- guzzlehttp/guzzle: ^6.3
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: @stable
- vlucas/phpdotenv: ~2.0
README
安装
您至少需要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。您可以设置自己的或使用当前的。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);
贡献
请打开PR,其中包含您对GitHub问题的更改。您的代码必须遵循PSR标准,并包含PHPUnit测试。