tsmall / php-json-rpc

Simple JSON-RPC server for PHP.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simple JSON-RPC System for PHP

This library provides the server side of a JSON-RPC system for PHP. It is designed to be simple to use and framework agnostic. You should be able to use it with any PHP web framework, or even without any framework at all.

Contents

  1. Usage Example
  2. Spec Compliance
  3. Errors Detected
  4. Planned Work

Usage Example

There are three steps required to use this library:

  1. Create the RPC system.

    use RPC\JsonRpcSystem;
    use RPC\Codec\JsonRequestDeserializer;
    use RPC\Codec\JsonResponseSerializer;
    
    $rpcSystem = new JsonRpcSystem(
        new JsonRequestDeserializer(),
        new JsonResponseSerializer()
    );
  2. Register one or more remote procedures.

    $rpcSystem->registerProcedure('hello', function($name) {
        return "Hello, $name!";
    });
  3. Call the procedures.

    $rpcRequest = [
        'jsonrpc' => '2.0',
        'method' => 'hi',
    ];
    $response = $this->rpcSystem->run($rpcRequest);

Spec Compliance

This library only supports a subset of the JSON-RPC 2.0 Specification. The list below shows the features implemented so far.

  • Calling a single procedure
  • Calling multiple procedures in batch
  • Named parameter lists
  • Notifications

Errors Detected

This library correctly handles every error case defined in the spec. It returns the correct error Json for each of the following:

  • Parse error
  • Invalid Request
  • Method not found
  • Invalid params
  • Internal error

Planned Work

It's rare for software to be "done". This library may get there some day. But until then, here is the currently planned work.

  • Make this library a proper PHP library that can be installed with Composer.

About

Simple JSON-RPC server for PHP.

License:MIT License


Languages

Language:PHP 100.0%