scottybo / linkedin-api-client-v2
LinkedIn API v2 客户端。处理 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
This package is not auto-updated.
Last update: 2024-09-17 20:40:19 UTC
README
这是一个正在进行中的工作 - 它还没有完成,所以不要使用它!!!
PHP LinkedIn API 客户端
一个用于处理 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发布时)。您可以使用 Guzzles 的 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的第三个参数是一个包含选项的数组。以下是您可以使用的数组键表。
更改请求格式
与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时可能的返回数据类型。
使用不同的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