artesaos/laravel-linkedin
Composer 安装命令:
composer require artesaos/laravel-linkedin
包简介
Linkedin API integration for Laravel and Lumen 5
README 文档
README
Linkedin API integration for Laravel Framework
This package is a wrapper for Happyr/LinkedIn-API-client. You can view the documentation for php version here. Don't forget to consult the oficial LinkedIn API site.
If you need install on Lumen, go to Lumen section
Installation on Laravel
Install with composer
composer require artesaos/laravel-linkedin
Add service Provider
Artesaos\LinkedIn\LinkedinServiceProvider::class,
Facade
'LinkedIn' => \Artesaos\LinkedIn\Facades\LinkedIn::class,
Publish config file
php artisan vendor:publish --provider="Artesaos\LinkedIn\LinkedinServiceProvider"
Installation on Lumen
Install with composer
composer require artesaos/laravel-linkedin
Add Service Provider, facade and config parameters to the bootstrap/app.php file and copy the linkedin.php to the config directory of your project (create then if not exists)
$app->register(\Artesaos\LinkedIn\LinkedinServiceProvider::class); class_alias(\Artesaos\LinkedIn\Facades\LinkedIn::class,'LinkedIn'); $app->configure('linkedin');
Usage
In order to use this API client (or any other LinkedIn clients) you have to register your app
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, please fill this values on linkedin.php config file.
####Basic Usage
The unique difference in this package is the LinkedIn facade. Instead of this:
$linkedIn=new Happyr\LinkedIn\LinkedIn('app_id', 'app_secret'); $linkedin->foo();
you can simple call the facade for anyone method, like this:
LinkedIn::foo();
or use the laravel container:
app('linkedin')->foo(); app()['linkedin']->foo(); App::make('linkedin')->foo(); // ...
The service container automatically return an instance of LinkedIn class ready to use
LinkedIn login
This example below is showing how to login with LinkedIn using LinkedIn facade.
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>"; exit();
Get basic profile info
You can retrive information using the get() method, like this:
LinkedIn::get('v1/people/~:(firstName,num-connections,picture-url)');
This query return an array of information. You can view all the REST api's methods in REST API Console
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::setAccessToken('access_token_from_db'); $options = ['json'=> [ 'comment' => 'Im testing Happyr LinkedIn client with Laravel Framework! https://github.com/artesaos/laravel-linkedin', 'visibility' => [ 'code' => 'anyone' ] ] ]; $result = LinkedIn::post('v1/people/~/shares', $options);
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. 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. |
| 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. |
| response_data_type | To override the response format for one request |
| query | This is an array with query parameters |
Changing request format
The default format when communicating with LinkedIn API is json. You can let the API do json_encode for you.
The following code shows you how.
$body = array( 'comment' => 'Testing the linkedin API!', 'visibility' => array('code' => 'anyone') ); LinkedIn::post('v1/people/~/shares', array('json'=>$body)); LinkedIn::post('v1/people/~/shares', array('body'=>json_encode($body)));
When using array('json'=>$body) as option the format will always be json. You can change the request format in three ways.
// By setter LinkedIn::setFormat('xml'); // Set format for just one request LinkedIn::post('v1/people/~/shares', array('format'=>'xml', 'body'=>$body));
Understanding response data type
The data type returned from LinkedIn::api can be configured. You may use the
LinkedIn::setResponseDataType or as an option for LinkedIn::api
// By setter LinkedIn::setResponseDataType('simple_xml'); // Set format for just one request LinkedIn::get('v1/people/~:(firstName,lastName)', array('response_data_type'=>'psr7'));
Below is a table that specifies what the possible return data types are when you call LinkedIn::api.
| Type | Description |
|---|---|
| array | An assosiative array. This can only be used with the json format. |
| simple_xml | A SimpleXMLElement. See PHP manual. This can only be used with the xml format. |
| psr7 | A PSR7 response. |
| stream | A file stream. |
| string | A plain old string. |
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)); return "<a href='$url'>Login with LinkedIn</a>";
Changelog
You can view the latest changes here
artesaos/laravel-linkedin 适用场景与选型建议
artesaos/laravel-linkedin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 66.47k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2016 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「client」 「sdk」 「linkedin」 「integration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 artesaos/laravel-linkedin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 artesaos/laravel-linkedin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 artesaos/laravel-linkedin 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
统计信息
- 总下载量: 66.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 61
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-14