bokokode/linkedin-v2-api-client

此包已被弃用,不再维护。未建议替代包。

1.4.1 2019-08-20 10:58 UTC

This package is auto-updated.

Last update: 2020-03-20 12:52:10 UTC


README

Latest Version Software License Build Status SensioLabsInsight Code Coverage Quality Score Total Downloads

一个用于处理与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>";

框架集成

如果您想要更容易地与框架集成,您可能想查看这些存储库