happyr/linkedin-api-client

LinkedIn API客户端。处理OAuth、CSRF保护。易于实现和扩展。这是一个为任何composer项目提供的独立库。

1.0.0 2016-08-15 10:35 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的第三个参数是一个包含选项的数组。以下是一个您可以使用数组键的表格。

更改请求格式

与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>";

框架集成

如果您想更容易地与框架集成,可以查看这些仓库: