Download the PHP package dan-da/json-rpc without Composer
On this page you can find all versions of the php package dan-da/json-rpc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dan-da/json-rpc
More information about dan-da/json-rpc
Files in dan-da/json-rpc
Package json-rpc
Short Description Simple Json-RPC client/server library that just works
License MIT
Homepage https://github.com/dan-da/JsonRPC
Informations about the package json-rpc
JsonRPC PHP Client and Server
A simple Json-RPC client/server that just works.
Complete fork of fguillot/JsonRPC
with latest changes.
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 >= 5.4
- License: MIT
This version forked by dan-da supports logging raw http requests/responses for debugging purposes and also returning unparsed json response to caller if desired.
Author
Frédéric Guillot
(This forked version modified by dan-da)
Installation with Composer
For PHP >= 5.4
Running Test Cases -- Optional
- git clone https://github.com/matasarei/JsonRPC.git
- cd JsonRPC
- composer install
- run vendor/bin/phpunit, eg:
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: