2017 © Pedro Peláez
 

library json-rpc

Library for building JSON-RPC 2.0 server

image

aveiv/json-rpc

Library for building JSON-RPC 2.0 server

  • Wednesday, July 29, 2015
  • by aveiv
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

aveiv/json-rpc

Library for building JSON-RPC 2.0 server., (*1)

Usage example

<?php

require __DIR__ . '/../vendor/autoload.php';

use Aveiv\JsonRpc\Exception\InvalidParamsException;
use Aveiv\JsonRpc\Exception\JsonRpcException;
use Aveiv\JsonRpc\Exception\MethodNotFoundException;
use Aveiv\JsonRpc\FrontController\FrontController;
use Aveiv\JsonRpc\MethodCaller\MethodCallerInterface;

class MethodCaller implements MethodCallerInterface
{
    /**
     * @param string $method
     * @param array $params
     * @param null|string|int|float $id
     * @return mixed
     * @throws InvalidParamsException
     * @throws JsonRpcException
     * @throws MethodNotFoundException
     */
    public function call($method, array $params = [], $id = null)
    {
        // your method call implementation here

        // for example:
        switch ($method) {
            case 'hello':
                return 'hello world';
            case 'sum':
                if (!isset($params['a']) || !isset($params['b'])) {
                    throw new InvalidParamsException();
                }
                $a = intval($params['a']);
                $b = intval($params['b']);
                return intval($a) + intval($b);
            case 'error':
                throw new JsonRpcException('Your error message', 999, 'Error data...');
            default:
                throw new MethodNotFoundException();
        }
    }
}

$methodCaller = new MethodCaller();
$frontController = new FrontController($methodCaller);
// if you need use jms/serializer for serialize result:
//$frontController = new FrontController($methodCaller, \JMS\Serializer\SerializerBuilder::create()->build());

$results = [];
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"hello","id":1}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"sum","id":2}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"sum","params":{"a":10,"b":20},"id":3}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"error","id":4}');
$results[] = $frontController->handle('{"jsonrpc":"2.0","method":"not_existing_method","id":5}');

print_r($results);

// prints:
//
// Array
// (
//     [0] => {"jsonrpc":"2.0","result":"hello world","id":1}
//     [1] => {"jsonrpc":"2.0","result":null,"error":{"code":-32602,"message":"Invalid params","data":null},"id":2}
//     [2] => {"jsonrpc":"2.0","result":30,"id":3}
//     [3] => {"jsonrpc":"2.0","result":null,"error":{"code":999,"message":"Your error message","data":"Error data..."},"id":4}
//     [4] => {"jsonrpc":"2.0","result":null,"error":{"code":-32601,"message":"Method not found","data":null},"id":5}
// )

The Versions

29/07 2015

dev-master

9999999-dev https://github.com/aveiv/json-rpc

Library for building JSON-RPC 2.0 server

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Alexandr Veliko-Ivanenko

json-rpc

29/07 2015

v1.0.0

1.0.0.0 https://github.com/aveiv/json-rpc

Library for building JSON-RPC 2.0 server

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Alexandr Veliko-Ivanenko

json-rpc