torann/api-client
Composer 安装命令:
composer require torann/api-client
包简介
A reusable base API client for use with remote services.
README 文档
README
A reusable base API client for use with remote services.
Installation
Install using composer:
$ composer require torann/api-client
Creating Clients
Once installed we need to create some clients. First we need to extend the \BaseApiClient\Client class and set endpoint namespace.
Client
<?php namespace App\BlogApi; class Client extends \BaseApiClient\Client { /** * Namespace for the endpoints * * @var string */ protected $endpointNamespace = 'App\BlogApi\Endpoints'; }
The $endpointNamespace variable is the prefix for the namespace of our endpoints.
Endpoints
From the endpoint we make our API calls and return the models.
<?php namespace App\BlogApi\Endpoints; use App\BlogApi\Models\Post; use BaseApiClient\Endpoint; use BaseApiClient\Models\Collection; class Posts extends Endpoint { /** * Get pages for the provided website. * * @param array $params * * @return Collection * @throws \BaseApiClient\Exceptions\ApiException */ public function index(array $params = []) { $response = $this->request->get('posts', $params); return new Collection($response, 'Post'); } /** * Create a new post. * * @param array $params * * @return Post * @throws \BaseApiClient\Exceptions\ApiException */ public function create(array $params) { $response = $this->request->post('posts', $params); return new Post($response); } /** * Delete the provided post. * * @param string $id * * @return bool * @throws \BaseApiClient\Exceptions\ApiException */ public function delete($id) { $response = $this->request->delete(sprintf('posts/%s', $id)); return $response->getResponseCode() === 200; } }
Models
<?php namespace App\BlogApi\Models; use BaseApiClient\Models\Model; class Post extends Model { /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = [ 'publish_at', ]; }
Registering Our Clients
<?php namespace App\Providers; use App\BlogApi\Client; use App\AnotherApi\Client; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // } /** * Register any application services. * * @return void */ public function register() { $this->registerBlogService(); $this->registerAnotherService(); } /** * Register blog manager services. * * @return void */ public function registerBlogService() { $this->app->bind(Client::class, function () { return new Client([ 'domain' => 'http://some.fancy.ip/', 'secret' => env('BLOG_MANAGER_API_SECRET'), ]); }); } /** * Register blog manager services. * * @return void */ public function registerAnotherService() { $this->app->bind(Client::class, function () { return new Client([ 'domain' => 'http://some.fancy.ip/', 'secret' => env('ANOTHER_API_SECRET'), ]); }); } }
Calling an Endpoint
Below is an example of using our \App\BlogApi\Client inside of a controller.
<?php namespace App\Http\Controllers; use App\BlogApi\Client; use Illuminate\Http\Request; class BlogController extends Controller { /** * Blog manager client instance. * * @var \App\BlogApi\Client */ protected $client; /** * Initializer constructor. * * @param Client $client */ public function __construct(Client $client) { parent::__construct(); $this->client = $client; } /** * Display the specified resource. * * @param \Illuminate\Http\Request $request * * @return \Illuminate\Http\Response */ public function index(Request $request) { $posts = $this->client->posts->index($request->only('page')); return view('posts.index')->with([ 'posts' => $posts->paginate() ]); } /** * Display the specified resource. * * @param Request $request * @param string $slug * * @return \Illuminate\Http\Response */ public function show(Request $request, $slug) { $post = $this->client->posts->find($slug); return view('posts.show')->with([ 'post' => $post ]); } }
Change Log
0.2.0
- Add support for Laravel 5.4
0.1.4
- Return null for empty values
0.1.3
- Add support for Laravel 5.3
0.1.2
- Remove trailing slash
0.1.1
- Bug fixes
0.1.0
- First release
torann/api-client 适用场景与选型建议
torann/api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 759 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「api」 「laravel」 「api client」 「base64 image」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 torann/api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 torann/api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 torann/api-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
Simple cURL wrapper
A PSR-7 compatible library for making CRUD API endpoints
A fluent PHP CURL wrapper
Build mini web servers in PHP for testing
Alfabank REST API integration
统计信息
- 总下载量: 759
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD
- 更新时间: 2016-03-10