vaderangry/php-json-rpc
Composer 安装命令:
composer require vaderangry/php-json-rpc
包简介
Flexible JSON-RPC2 server/client implementation for PHP7
关键字:
README 文档
README
Flexible JSON-RPC2 server/client implementation for PHP7.
Features
- JSON-RPC 2.0 full conformance (batch requests, notification, positional and named arguments, etc).
- Quick-start with default routing based on php namespaces.
- Flexible custom routing for your requirements.
- The mechanism of intercepting requests and responses through handlers.
- Automatic casting types in requests and responses.
- Fully unit tested.
Installation
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require vaderangry/php-json-rpc
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Documentation
Basic usage
Server
The example of quick-start:
<?php use PhpJsonRpc\Server; // Class for which provide JSON-RPC2 API: class Math { public function pow(float $base, int $exp): float { return pow($base, $exp); } } $server = new Server(); $response = $server ->addHandler(new Math()) ->execute(); echo $response;
Method Math::pow by default will mapped to method Math.pow in JSON-RPC2 terms. Request example:
{
"jsonrpc": "2.0",
"method": "Math.pow",
"params": {"base": 2, "exp": 3},
"id": 1
}
The example of custom method mapping:
<?php use PhpJsonRpc\Server; use PhpJsonRpc\Server\MapperInterface; // Define custom mapper class Mapper implements MapperInterface { public function getClassAndMethod(string $requestedMethod): array { // Keys of array presents requested method $map = [ 'pow' => [Math::class, 'pow'], ]; if (array_key_exists($requestedMethod, $map)) { return $map[$requestedMethod]; } return ['', '']; } } $server = new Server(); // Register new mapper $server->setMapper(new Mapper()); // Register handler and run server $response = $server->addHandler(new Math())->execute(); echo $response;
Now Math::pow will be mapped to pow. Request example:
{
"jsonrpc": "2.0",
"method": "pow",
"params": {"base": 2, "exp": 3},
"id": 1
}
Client
Single request:
<?php use PhpJsonRpc\Client; $client = new Client('http://localhost'); $result = $client->call('Math.pow', [2, 3]); // $result = 8 $result = $client->call('Math.methodNotFound', []); // $result = null
Single request with exception server error mode:
<?php use PhpJsonRpc\Client; $client = new Client('http://localhost', Client::ERRMODE_EXCEPTION); $result = $client->call('Math.methodNotFound', []); // throw MethodNotFoundException
Batch request:
<?php use PhpJsonRpc\Client; $client = new Client('http://localhost'); $result = $client->batch() ->call('Util.Math.pow', [2, 1]) ->call('Util.Math.pow', [2, 2]) ->call('Util.Math.pow', [2, 3]) ->call('Util.methodNotFound', []) ->batchExecute(); // $result = [2, 4, 8, null]
All unit of result stored at the same position of call. Server error present null object.
Batch request with exception server error mode:
<?php use PhpJsonRpc\Client; $client = new Client('http://localhost', Client::ERRMODE_EXCEPTION); $result = $client->batch() ->call('Util.Math.pow', [2, 1]) ->call('Util.methodNotFound', []) ->batchExecute(); // $result = [2, MethodNotFoundException]
With exception mode server error present JsonRpcException object.
Exception will not throw for saving other units of result.
The example with personal custom headers in request:
<?php use PhpJsonRpc\Client; use PhpJsonRpc\Common\Interceptor\Container; use PhpJsonRpc\Common\Interceptor\Interceptor; $client = new Client('http://localhost'); $client->getTransport()->onPreRequest() ->add(Interceptor::createWith(function (Container $container) { // Get transport from container $transport = $container->first(); // Add required headers $transport->addHeaders([ "Origin: " . $_SERVER['HTTP_HOST'], ]); // Now we MUST return container for next chain return new Container($transport, $container->last()); })); $result = $client->call('Math.pow', [2, 3]); // $result = 8
Tests
$ ./vendor/bin/phpunit -c ./
vaderangry/php-json-rpc 适用场景与选型建议
vaderangry/php-json-rpc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.89k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2016 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rpc」 「json」 「json-rpc2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaderangry/php-json-rpc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaderangry/php-json-rpc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaderangry/php-json-rpc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
JSON RPC 2.0 for Yii2 strict type validation of request and response data
Microservice RPC through message queues.
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
JSON RPC 2.0 for Yii2 API with CRUD+ actions
统计信息
- 总下载量: 7.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-28