convidera / linkedin-api-client
LinkedIn API客户端。处理OAuth,CSRF保护。易于实现和扩展。这是任何composer项目的独立库。
Requires
- php: ^5.5 || ^7.0
- php-http/client-implementation: ^1.0
- php-http/discovery: ^1.0
- php-http/httplug: ^1.0
- php-http/message-factory: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.2
- illuminate/support: ^5.0
- mockery/mockery: ^0.9
- php-http/guzzle5-adapter: ^1.0
- phpunit/phpunit: ^4.5 || ^5.0
README
一个用于处理与LinkedIn API进行认证和通信的PHP库。该库/SDK帮助您获取访问令牌,在认证后,它帮助您发送API请求。但您不会免费得到所有东西... 您必须阅读LinkedIn文档来了解您应该如何查询API。
为了了解此库实际上为您做了什么。请查看API文档中的认证页面。
特性
以下是一些特性,可能会让您选择这个LinkedIn客户端而不是我们的一些竞争对手。
- 灵活且易于扩展
- 使用现代PHP标准开发
- 不是为特定框架开发的。
- 处理认证过程
- 尊重CSRF保护
安装
TL;DR
composer require php-http/curl-client guzzlehttp/psr7 php-http/message happyr/linkedin-api-client
此库不依赖于Guzzle或其他发送HTTP请求的库。我们使用出色的HTTPlug来实现解耦。我们希望您选择用于发送HTTP请求的库。查看支持php-http/client-implementation的包列表,以找到要使用的客户端。有关虚拟包的更多信息,请参阅HTTPlug。示例
composer require php-http/guzzle6-adapter
您还需要安装一个PSR-7实现和创建PSR-7消息的工厂(PSR-17发布时)。您可以使用Guzzle的PSR-7实现和php-http的工厂。
composer require guzzlehttp/psr7 php-http/message
现在您可以运行以下命令来安装库
composer require happyr/linkedin-api-client
如果您是从先前的版本更新,请确保阅读升级文档。
找到HTTP客户端(可选)
LinkedIn客户端需要知道您用于发送HTTP消息的库。您可以提供一个HttpClient实例和MessageFactory实例,或者可以回退到自动发现。以下是一个提供Guzzle6实例的示例。
$linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret'); $linkedIn->setHttpClient(new \Http\Adapter\Guzzle6\Client()); $linkedIn->setHttpMessageFactory(new Http\Message\MessageFactory\GuzzleMessageFactory());
使用
为了使用此API客户端(或任何其他LinkedIn客户端),您必须在LinkedIn上注册您的应用程序以接收API密钥。一旦您注册了LinkedIn应用程序,您将获得一个API密钥和密钥。
LinkedIn登录
以下示例显示了如何使用LinkedIn进行登录。
<?php /** * This demonstrates how to authenticate with LinkedIn and send api requests */ /* * First you need to make sure you've used composers auto load. You have is probably * already done this before. You usually don't bother.. */ //require_once "vendor/autoload.php"; $linkedIn=new Happyr\LinkedIn\LinkedIn('client_id', 'client_secret'); if ($linkedIn->isAuthenticated()) { //we know that the user is authenticated now. Start query the API $user=$linkedIn->get('v1/people/~:(firstName,lastName)'); echo "Welcome ".$user['firstName']; exit(); } elseif ($linkedIn->hasError()) { echo "User canceled the login."; exit(); } //if not authenticated $url = $linkedIn->getLoginUrl(); echo "<a href='$url'>Login with LinkedIn</a>";
如何在LinkedIn墙上发布
以下示例显示了如何在用户的墙上发布。访问令牌从数据库中获取。
$linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret'); $linkedIn->setAccessToken('access_token_from_db'); $options = array('json'=> array( 'comment' => 'Im testing Happyr LinkedIn client! https://github.com/Happyr/LinkedIn-API-client', 'visibility' => array( 'code' => 'anyone' ) ) ); $result = $linkedIn->post('v1/people/~/shares', $options); var_dump($result); // Prints: // array (size=2) // 'updateKey' => string 'UPDATE-01234567-0123456789012345678' (length=35) // 'updateUrl' => string 'https://www.linkedin.com/updates?discuss=&scope=01234567&stype=M&topic=0123456789012345678&type=U&a=mVKU' (length=104)
当然,您也可以使用xml进行同样的操作。使用以下选项数组。
$options = array( 'format' => 'xml', 'body' => '<share> <comment>Im testing Happyr LinkedIn client! https://github.com/Happyr/LinkedIn-API-client</comment> <visibility> <code>anyone</code> </visibility> </share>');
配置
API选项
LinkedIn::api的第三个参数是一个包含选项的数组。以下是您可能使用的数组键的表格。
| 选项名称 | 描述 |
|---|---|
| body | HTTP请求的主体。在这里放入您的xml字符串。 |
| format | 将此设置为'json'、'xml'或'simple_xml'以覆盖默认值。 |
| headers | 这是请求的HTTP头部信息 |
| json | 这是一个包含要编码为JSON字符串的JSON数据的数组。使用此选项时,您需要指定一个格式。 |
| response_data_type | 用于覆盖单个请求的响应格式 |
| query | 这是一个包含查询参数的数组 |
更改请求格式
与LinkedIn API通信的默认格式是json。您可以让API为您执行json_encode。以下代码展示了如何操作。
$body = array( 'comment' => 'Im testing Happyr LinkedIn client! https://github.com/Happyr/LinkedIn-API-client', 'visibility' => array('code' => 'anyone') ); $linkedIn->post('v1/people/~/shares', array('json'=>$body)); $linkedIn->post('v1/people/~/shares', array('body'=>json_encode($body)));
当使用array('json' => $body)作为选项时,格式始终为json。您可以通过三种方式更改请求格式。
// By constructor argument $linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret', 'xml'); // By setter $linkedIn->setFormat('xml'); // Set format for just one request $linkedIn->post('v1/people/~/shares', array('format'=>'xml', 'body'=>$body));
理解响应数据类型
从LinkedIn::api返回的数据类型可以进行配置。您可以使用第四个构造函数参数、LinkedIn::setResponseDataType或作为LinkedIn::api的选项。
// By constructor argument $linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret', 'json', 'array'); // By setter $linkedIn->setResponseDataType('simple_xml'); // Set format for just one request $linkedIn->get('v1/people/~:(firstName,lastName)', array('response_data_type'=>'psr7'));
以下表格指定了调用LinkedIn::api时可能的返回数据类型。
| 类型 | 描述 |
|---|---|
| array | 一个关联数组。这只能与json格式一起使用。 |
| simple_xml | 一个SimpleXMLElement。参见PHP手册。这只能与xml格式一起使用。 |
| psr7 | 一个PSR7响应。 |
| stream | 一个文件流。 |
| string | 一个普通的字符串。 |
使用不同的Session类
您可能想使用除了默认的SessionStorage之外的存储。如果您使用Laravel,您更有可能注入IlluminateSessionStorage。
$linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret'); $linkedIn->setStorage(new IlluminateSessionStorage());
您可以注入任何实现了DataStorageInterface的类。您还可以注入不同的UrlGenerator类。
使用不同的作用域
如果您想在认证用户时定义特殊的作用域,您应该在生成登录URL时指定它们。如果不指定作用域,LinkedIn将使用为应用程序配置的默认作用域。
$scope = 'r_fullprofile,r_emailaddress,w_share'; //or $scope = array('rw_groups', 'r_contactinfo', 'r_fullprofile', 'w_messages'); $url = $linkedIn->getLoginUrl(array('scope'=>$scope)); echo "<a href='$url'>Login with LinkedIn</a>";
框架集成
如果您想要更容易地与框架集成,您可能想查看以下存储库
- HappyrLinkedInBundle 用于Symfony
- Laravel-Linkedin by mauri870 用于Laravel 5