Download the PHP package kba-team/json-rpc without Composer
On this page you can find all versions of the php package kba-team/json-rpc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kba-team/json-rpc
More information about kba-team/json-rpc
Files in kba-team/json-rpc
Package json-rpc
Short Description Simple JSON-RPC client/server library that just works
License MIT
Homepage https://github.com/the-kbA-team/json-rpc/
Informations about the package json-rpc
JSON-RPC PHP Client and Server
A simple JSON-RPC client/server that just works.
This is fork of matasarei/JsonRPC
which in turn is a fork of
fguillot/JsonRPC
.
Features
- JSON-RPC 2.0 only
- The server support batch requests and notifications
- Authentication and IP based client restrictions
- Custom Middleware
- Fully unit tested
- Requirements: PHP >= 7.4
- License: MIT
Author
Frédéric Guillot
Installation with Composer
For PHP >= 7.4
Running Test Cases
- Clone the repo
cd JsonRPC
- Run composer:
composer install
- Run tests:
vendor/bin/phpunit
Examples
Server
Callback binding:
Callback binding from array:
Class/Method binding:
Class/Method binding from array:
Server Middleware:
Middleware might be used to authenticate and authorize the client. They are executed before each procedure.
You can raise a AuthenticationFailureException
when the API credentials are wrong or a AccessDeniedException
when the user is not allowed to access to the procedure.
Client
Example with positional parameters:
Example with named arguments:
Arguments are called in the right order.
Examples with the magic method __call()
:
The example above use positional arguments for the request and this one use named arguments:
Client batch requests
Call several procedures in a single HTTP request:
All results are stored at the same position of the call.
Client exceptions
Client exceptions are normally thrown when an error is returned by the server. You can change this behaviour by
using the $returnException
argument which causes exceptions to be returned. This can be extremely useful when
executing the batch request.
BadFunctionCallException
: Procedure not found on the serverInvalidArgumentException
: Wrong procedure argumentsJsonRPC\Exception\AccessDeniedException
: Access deniedJsonRPC\Exception\ConnectionFailureException
: Connection failureJsonRPC\Exception\ServerErrorException
: Internal server error
Enable client debugging
You can enable the debug mode to see the JSON request and response:
The debug output is sent to the PHP system logger.
You can configure the log destination in your php.ini
.
Output example:
IP based client restrictions
The server can allow only some IP addresses:
If the client is blocked, you got a 403 Forbidden HTTP response.
HTTP Basic Authentication
If you use HTTPS, you can allow client by using a username/password.
On the client, set credentials like that:
If the authentication failed, the client throw a RuntimeException.
Using an alternative authentication header:
The example above will use the HTTP header X-Authentication
instead of the standard Authorization: Basic [BASE64_CREDENTIALS]
.
The username/password values need be encoded in base64: base64_encode('username:password')
.
Local Exceptions
By default, the server will relay all exceptions to the client.
If you would like to relay only some of them, use the method Server::withLocalException($exception)
:
Callback before client request
You can use a callback to change the HTTP headers or the URL before to make the request to the server.
Example:
All versions of json-rpc with dependencies
ext-json Version *