Download the PHP package rambler-digital-solutions/php-json-rpc without Composer
On this page you can find all versions of the php package rambler-digital-solutions/php-json-rpc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rambler-digital-solutions/php-json-rpc
More information about rambler-digital-solutions/php-json-rpc
Files in rambler-digital-solutions/php-json-rpc
Package php-json-rpc
Short Description Simple Json-RPC client/server library that just works
License MIT
Homepage https://github.com/rambler-digital-solutions/php-json-rpc
Informations about the package php-json-rpc
JsonRPC PHP Client and Server
A simple Json-RPC client/server that just works. Forked from fguillot/JsonRPC while trying to keep it slim and speedy.
Features
- JSON-RPC 2.0 protocol only
- The server support batch requests and notifications
- Authentication and IP based client restrictions
- Minimalist: there is only 2 files
- Fully unit tested
- Requirements: PHP >= 5.6
- License: MIT
Author
Frédéric Guillot
Installation with Composer
Examples
Server
Callback binding:
Class/Method binding:
Before callback:
Before each procedure execution, a custom method can be called.
This method receive the following arguments: $username, $password, $class, $method
.
You can use this method to implements a custom authentication system or anything else.
If you would like to reject the authentication, you can throw the exception JsonRPC\AuthenticationFailure
.
Client
Example with positional parameters:
Example with named arguments:
Arguments are called in the right order.
Examples with shortcut methods:
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 'suppress_errors' option 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\AccessDeniedException
: Access deniedJsonRPC\ConnectionFailureException
: Connection failureJsonRPC\ServerErrorException
: Internal server errorRuntimeException
: Protocol error
Enable client debugging
You can enable the debug to see the JSON request and response:
The debug output is sent to the PHP's 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 adresses:
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')
.
Exceptions
If you want to send an error to the client you can throw an exception. You should configure which exceptions should be relayed to the client first:
Then you can throw that exception inside your procedure:
To relay all exceptions regardless of type, leave out the exception class name: