PHP code example of operation-hardcode / php-rpc-server

1. Go to this page and download the library: Download operation-hardcode/php-rpc-server 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/ */

    

operation-hardcode / php-rpc-server example snippets



use OperationHardcode\PhpRpcServer\RpcServer;
use OperationHardcode\PhpRpcServer\RpcRequest;
use OperationHardcode\PhpRpcServer\RpcResponse;
use OperationHardcode\PhpRpcServer\Protocol\Validation\ValidateRequest;

$server = RpcServer::new([
    'users.get' => function (RpcRequest $request, ?RpcResponse $response = null): ?RpcResponse {
        return $response?->addResult([10, 11]);
    }
])

$server->process('{"jsonrpc": "2.0", "method": "users.get", "id": 1}');


use OperationHardcode\PhpRpcServer\RpcServer;
use OperationHardcode\PhpRpcServer\RpcRequest;
use OperationHardcode\PhpRpcServer\RpcResponse;
use OperationHardcode\PhpRpcServer\Protocol\Validation\ValidateRequest;
use Psr\Log\NullLogger;

final class CustomValidator implements ValidateRequest
{
    /** {@inheritdoc} */
    public function validate(array $payload): bool
    {
        return false;
    }
}

$server = RpcServer::new([
    'users.get' => function (RpcRequest $request, ?RpcResponse $response = null): ?RpcResponse {
        return $response?->addResult([10, 11]);
    }
], new NullLogger(), [new CustomValidator()])

$server->process('{"jsonrpc": "2.0", "method": "users.get", "id": 1}');


use OperationHardcode\PhpRpcServer\RpcHandler;
use OperationHardcode\PhpRpcServer\RpcRequest;
use OperationHardcode\PhpRpcServer\RpcResponse;
use OperationHardcode\PhpRpcServer\RpcServer;

final class YourOwnRpcHandler implements RpcHandler
{
    public function handle(RpcRequest $request, ?RpcResponse $response = null) : ?RpcResponse
    {
        //
    }
}

$server = new RpcServer(new YourOwnRpcHandler());

$server->process('{"jsonrpc": "2.0", "method": "users.get", "id": 1}');