Download the PHP package srjlewis/json-rpc without Composer
On this page you can find all versions of the php package srjlewis/json-rpc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download srjlewis/json-rpc
More information about srjlewis/json-rpc
Files in srjlewis/json-rpc
Package json-rpc
Short Description Simple Json-RPC client/server library that just works
License MIT
Homepage https://github.com/srjlewis/JsonRPC
Informations about the package json-rpc
JsonRPC PHP Client and Server
A simple Json-RPC client/server that just works.
Forked From fguillot/json-rpc
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.1|8
- License: MIT
Author
Original Author: Frédéric Guillot
Steven Lewis
Installation with Composer
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: