net-shell/linkedin-api
Composer 安装命令:
composer require net-shell/linkedin-api
包简介
LinkedIn API client. Handles OAuth, CSRF protection. Easy to implement and extend..
README 文档
README
A PHP library to handle authentication and communication with LinkedIn API. The library/SDK helps you to get an access token and when authenticated it helps you to send API requests. You will not get everything for free though... You have to read the LinkedIn documentation to understand how you should query the API.
To get an overview what this library actually is doing for you. Take a look at the authentication page from the API docs.
Features
Here is a list of features that might convince you to choose this LinkedIn client over some of our competitors'.
- Flexible and easy to extend
- Developed with modern PHP standards
- Not developed for a specific framework.
- Handles the authentication process
- Respects the CSRF protection
- More than 85% test coverage.
- 800 lines of code, 650 lines of comments.
Installation
Install it with Composer.
composer require net-shell/linkedin-api:dev-master
Usage
In order to use this API client (or any other LinkedIn clients) you have to register your application with LinkedIn to receive an API key. Once you've registered your LinkedIn app, you will be provided with an API Key and Secret Key.
LinkedIn login
This example below is showing how to login with 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('app_id', 'app_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>";
How to post on LinkedIn wall
The example below shows how you can post on a users wall. The access token is fetched from the database.
$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)
You may of course do the same in xml. Use the following options array.
$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>');
Configuration
The api options
The third parameter of LinkedIn::api is an array with options. They will eventually be past to a Request client but
before that we do some modifications. Below is a table of array keys that you may use.
| Option name | Description |
|---|---|
| body | The body of a HTTP request. Put your xml string here. |
| debug | This will echo the all the request and response headers. (Works with the Guzzle client only) |
| format | Set this to 'json', 'xml' or 'simple_xml' to override the default value. |
| headers | This is HTTP headers to the request |
| json | This is an array with json data that will be encoded to a json string. Using this option you do need to specify a format. |
| query | This is an array with query parameters |
If you are using the GuzzleRequest (default) you may want to have a look at its documentation
to find out what more options that are available.
Changing format
The default format when communicating with LinkedIn API is json. This means that you will get an array back as a response when you call LinkedIn::api. It is easy to change the format.
// 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->get('v1/people/~:(firstName,lastName)', array('format'=>'xml'));
There is one exception to the format option: When you specifying $options['json'=>...] then the format will always be json.
Below is a table that specifies what format returns what when you call LinkedIn::api.
| Format | Return value |
|---|---|
| json | An assosiative array |
| xml | The XML response body as a string |
| simple_xml | A SimpleXMLElement. See PHP manual. |
Use different Request or Session classes
You might want to use an other storage than the default SessionStorage. If you are using Laravel
you are more likely to inject the IlluminateSessionStorage.
$linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret'); $linkedIn->setStorage(new IlluminateSessionStorage());
You can inject any class implementing DataStorageInterface. You can also inject different
request and url generator classes.
Using different scopes
If you want to define special scopes when you authenticate the user you should specify them when you are generating the login url. If you don't specify scopes LinkedIn will use the default scopes that you have configured for the app.
$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>";
Framework integration
See how I integrated this with Symfony2.
net-shell/linkedin-api 适用场景与选型建议
net-shell/linkedin-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「client」 「oauth」 「sdk」 「linkedin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 net-shell/linkedin-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 net-shell/linkedin-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 net-shell/linkedin-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A PSR-7 compatible library for making CRUD API endpoints
Mock PSR-18 HTTP client
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-14