https://github.com/vaderangry/phpjsonrpc
https://github.com/vaderangry/phpjsonrpc
api batch-request json-rpc2 php php7
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/vaderangry/phpjsonrpc
- Owner: vaderangry
- License: mit
- Created: 2016-10-30T15:12:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-02T13:12:32.000Z (about 8 years ago)
- Last Synced: 2024-04-28T17:04:28.777Z (almost 2 years ago)
- Topics: api, batch-request, json-rpc2, php, php7
- Language: PHP
- Size: 86.9 KB
- Stars: 10
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PhpJsonRpc
Flexible [JSON-RPC2](http://www.jsonrpc.org/specification) server/client implementation for PHP7.
[](https://scrutinizer-ci.com/g/vaderangry/PhpJsonRpc/?branch=master)
[](https://travis-ci.org/vaderangry/PhpJsonRpc)
## 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:
```console
$ composer require vaderangry/php-json-rpc
```
This command requires you to have Composer installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the Composer documentation.
## Documentation
- [Server usage](doc/01-server-usage.md)
- [Client usage](doc/02-client-usage.md)
## Basic usage
### Server
The example of quick-start:
```php
addHandler(new Math())
->execute();
echo $response;
```
Method `Math::pow` by default will mapped to method `Math.pow` in JSON-RPC2 terms. Request example:
```json
{
"jsonrpc": "2.0",
"method": "Math.pow",
"params": {"base": 2, "exp": 3},
"id": 1
}
```
The example of custom method mapping:
```php
[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:
```json
{
"jsonrpc": "2.0",
"method": "pow",
"params": {"base": 2, "exp": 3},
"id": 1
}
```
### Client
Single request:
```php
call('Math.pow', [2, 3]); // $result = 8
$result = $client->call('Math.methodNotFound', []); // $result = null
```
Single request with exception server error mode:
```php
call('Math.methodNotFound', []); // throw MethodNotFoundException
```
Batch request:
```php
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
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
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
```Bash
$ ./vendor/bin/phpunit -c ./
```