The server is a complete implementation of the JSON-RPC 2.0 specification. The server will expose public methods of an object or a static class which can be optionally limited using an interface or specific parent class.
To encourage interface segregation there is no support for other methods like closures or global functions.
- Full specification including notifications, both parameters by name and by position and batch requests
- Default values are used for skipped arguments
- Variadic arguments are supported
- PSR-7 compatible: This server can directly handle implementations of
Psr\Http\Message\ServerRequestInterface
, returning an implementation ofPsr\Http\Message\ResponseInterface
, as defined in PSR-7
PHP >= 8.0
<?php
interface MyInterface
{
public function foo();
}
<?php
class MySubjectClass implements MyInterface
{
public function foo()
{
return 'foo';
}
public function bar()
{
return 'bar';
}
}
<?php
use Procurios\Json\JsonRpc\Server;
use Procurios\Json\JsonRpc\Request\Request;
$requestData = json_decode(file_get_contents('php://input'), true);
$Request = Request::fromArray($requestData);
$Server = new Server(new MySubjectClass);
$Response = $Server->handleRequest($Request);
header('Content-Type: application/json');
die($Response->asString());
<?php
use Procurios\Json\JsonRpc\Server;
$Server = new Server(new MySubjectClass);
// Use the current Psr\Http\Message\ServerRequestInterface implementation in your application
$Request = MyRequestSource::getRequest();
// Create an empty implementation of Psr\Http\Message\ResponseInterface
$BaseResponse = MyResponseFactory::createResponse();
$Response = $Server->handleServerRequest($Request, $BaseResponse);
MyResponseEmitter::emit($Response);
<?php
use Procurios\Json\JsonRpc\Server;
$Server = new Server(new MySubjectClass, MyInterface::class);
// Only the method foo will be available in this server, since bar is not part of the interface