PHP code example of procurios / json-rpc

1. Go to this page and download the library: Download procurios/json-rpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

procurios / json-rpc example snippets



interface MyInterface
{
    public function foo();
}


class MySubjectClass implements MyInterface
{
    public function foo()
    {
        return 'foo';
    }

    public function bar()
    {
        return 'bar';
    }
}


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());


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);


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