Php-rpc-server - JSON RPC server implementation for PHP.

Overview

test Codecov Software License Total Downloads

JSON RPC Server implementation for PHP.

The json-rpc is a very simple protocol. You can see this by reading the protocol specification.

This library implements json-rpc server specification. It is easy to use and well typed.

Contents

Installation

composer require operation-hardcode/php-rpc-server

Usage

Since the json-rpc server has only one endpoint, you need a handler that knows how to determine from the name of the method the handler that you will handle the request. You can write it yourself, if the OperationHardcode\PhpRpcServer\RpcHandler interface is implemented. Or use the ready-made one that comes with the library, the OperationHardcode\PhpRpcServer\InvokableRpcHandler.

To start to use the rpc server, instantiate the RpcServer:

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

The rpc-server does not fetch the json, it just processes it according to the specification. This gives you the freedom to use the server, because you can pass the json fetched any way you know: via http, web sockets or even via tcp.

As you may have noticed, handlers may not have an RpcResponse object. If so, it is a notification, not a request, so no response to the client will follow. You can simply return this object (because it is null) or null directly.

The rpc-server uses validators to validate json against the specification. If you want to add your own validators, you must implement the OperationHardcode\PhpRpcServer\Protocol\Validation\ValidateRequest interface. In the validate method you will get the raw payload to ensure it satisfy your extended protocol rules.

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

If your validator fails, the error INVALID_REQUEST will be returned to the client.

If you are not satisfied with the standard request handler, you can write your own.

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

Examples

More extensive code examples reside in the examples directory.

Testing

$ composer test

Stat Analysis

$ composer lint

License

The MIT License (MIT). See License File for more information.

You might also like...
A cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services.

Motan Overview Motan is a cross-language remote procedure call(RPC) framework for rapid development of high performance distributed services. Related

Tars is a high-performance RPC framework based on name service and Tars protocol, also integrated administration platform, and implemented hosting-service via flexible schedule.

TARS - A Linux Foundation Project TARS Foundation Official Website TARS Project Official Website WeChat Group: TARS01 WeChat Offical Account: TarsClou

Simple game server with php without socket programming. Uses the Api request post(json).

QMA server Simple game server with php without socket programming. Uses the Api request post(json). What does this code do? Register the user as a gue

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.
A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

A pure PHP implementation of the open Language Server Protocol. Provides static code analysis for PHP for any IDE.

An open-source Minecraft: Java Edition server implementation, written in PHP.
An open-source Minecraft: Java Edition server implementation, written in PHP.

PHPCraft An open-source Minecraft: Java Edition server implementation, written in PHP. What is PHPCraft? PHPCraft is an open-source Minecraft: Java Ed

JSON Lint for PHP

JSON Lint Usage use Seld\JsonLint\JsonParser; $parser = new JsonParser(); // returns null if it's valid json, or a ParsingException object. $parser-

 Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC from scratch
Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC from scratch

This Project is based on course CSC 3215. Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC (Model–View–Controller) from scratch. Just learning about web technologies, Not focusing on UI (Bootstrap or other 3rd-Party UI libraries or frameworks).

Declaratively specify how to extract elements from a JSON document, in PHP

jmespath.php JMESPath (pronounced "jaymz path") allows you to declaratively specify how to extract elements from a JSON document. jmespath.php allows

Dependency graph visualization for composer.json (PHP + Composer)
Dependency graph visualization for composer.json (PHP + Composer)

clue/graph-composer Graph visualization for your project's composer.json and its dependencies: Table of contents Usage graph-composer show graph-compo

Releases(v0.1.0)
Owner
null
A light-weight RPC implement of google protobuf RPC framework.

sofa-pbrpc A light-weight RPC implementation of Google's protobuf RPC framework. Wiki: https://github.com/baidu/sofa-pbrpc/wiki Features High performa

Baidu 2.1k Dec 10, 2022
Zilliqa PHP is a typed PHP-7.1+ interface to Zilliqa JSON-RPC API.

Zilliqa PHP is a typed PHP-7.1+ interface to Zilliqa JSON-RPC API. Check out the latest API documentation. Add library in a composer.json file.

Patrick Falize 6 Oct 7, 2021
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
Json-normalizer: Provides generic and vendor-specific normalizers for normalizing JSON documents

json-normalizer Provides generic and vendor-specific normalizers for normalizing JSON documents. Installation Run $ composer require ergebnis/json-nor

null 64 Dec 31, 2022
PDF API. JSON to PDF. PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data

PDF Template Management, Visual HTML Template Editor and API to render PDFS by json data PDF ENGINE VERSION: development: This is a prerelease version

Ajous Solutions 2 Dec 30, 2022
Swaggest JSON-schema implementation for PHP

Swaggest JSON-schema implementation for PHP High definition PHP structures with JSON-schema based validation. Supported schemas: JSON Schema Draft 7 J

null 370 Dec 29, 2022
A Magento implementation for validating JSON Structures against a given Schema

Zepgram JsonSchema A Magento implementation for validating JSON Structures against a given Schema with support for Schemas of Draft-3 or Draft-4. Base

Benjamin Calef 1 Nov 5, 2021
Hprose is a cross-language RPC. This project is Hprose 3.0 for PHP

Hprose 3.0 for PHP Introduction Hprose is a High Performance Remote Object Service Engine. It is a modern, lightweight, cross-language, cross-platform

Hprose 2k Jan 4, 2023
Hprose RPC 服务 in Laravel/Lumen

基于 hprose/hprose-php 开发的Laravel扩展:whereof/laravel-hprose

wangzhiqiang 6 Mar 18, 2022
Simple XML-RPC API

Readme Ripcord: Easy XML-RPC Client and Server for PHP 5 ========================================================================

Saravanakumar Arumugam 1 Nov 22, 2021