2017 © Pedro Peláez
 

library php-json-rpc

Flexible JSON-RPC2 server/client implementation for PHP7

image

vaderangry/php-json-rpc

Flexible JSON-RPC2 server/client implementation for PHP7

  • Friday, March 2, 2018
  • by vaderangry
  • Repository
  • 3 Watchers
  • 4 Stars
  • 1,935 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 3 % Grown

The README.md

PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7., (*1)

Scrutinizer Code Quality Build Status, (*2)

Features

  • JSON-RPC 2.0 full conformance (batch requests, notification, positional and named arguments, etc)​.
  • Quick-start with default routing based on php namespaces.
  • Flexible custom routing for your requirements.
  • The mechanism of intercepting requests and responses through handlers.
  • Automatic casting types in requests and responses.
  • Fully unit tested.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*3)

$ composer require vaderangry/php-json-rpc

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*4)

Documentation

Basic usage

Server

The example of quick-start:, (*5)

<?php

use PhpJsonRpc\Server;

// Class for which provide JSON-RPC2 API:
class Math
{
    public function pow(float $base, int $exp): float
    {
        return pow($base, $exp);
    }
}

$server = new Server();

$response = $server
    ->addHandler(new Math())
    ->execute();

echo $response;

Method Math::pow by default will mapped to method Math.pow in JSON-RPC2 terms. Request example:, (*6)

{
  "jsonrpc": "2.0", 
  "method": "Math.pow", 
  "params": {"base": 2, "exp": 3}, 
  "id": 1
}

The example of custom method mapping:, (*7)

<?php

use PhpJsonRpc\Server;
use PhpJsonRpc\Server\MapperInterface;

// Define custom mapper
class Mapper implements MapperInterface
{
    public function getClassAndMethod(string $requestedMethod): array
    {
        // Keys of array presents requested method
        $map = [
            'pow' => [Math::class, 'pow'],
        ];

        if (array_key_exists($requestedMethod, $map)) {
            return $map[$requestedMethod];
        }

        return ['', ''];
    }
}

$server = new Server();

// Register new mapper
$server->setMapper(new Mapper());

// Register handler and run server
$response = $server->addHandler(new Math())->execute();

echo $response;

Now Math::pow will be mapped to pow. Request example:, (*8)

{
  "jsonrpc": "2.0", 
  "method": "pow", 
  "params": {"base": 2, "exp": 3}, 
  "id": 1
}

Client

Single request:, (*9)

<?php

use PhpJsonRpc\Client;

$client = new Client('http://localhost');
$result = $client->call('Math.pow', [2, 3]); // $result = 8
$result = $client->call('Math.methodNotFound', []); // $result = null

Single request with exception server error mode:, (*10)

<?php

use PhpJsonRpc\Client;

$client = new Client('http://localhost', Client::ERRMODE_EXCEPTION);
$result = $client->call('Math.methodNotFound', []); // throw MethodNotFoundException

Batch request:, (*11)

<?php

use PhpJsonRpc\Client;

$client = new Client('http://localhost');

$result = $client->batch()
    ->call('Util.Math.pow', [2, 1])
    ->call('Util.Math.pow', [2, 2])
    ->call('Util.Math.pow', [2, 3])
    ->call('Util.methodNotFound', [])
    ->batchExecute();
// $result = [2, 4, 8, null]

All unit of result stored at the same position of call. Server error present null object., (*12)

Batch request with exception server error mode:, (*13)

<?php

use PhpJsonRpc\Client;

$client = new Client('http://localhost', Client::ERRMODE_EXCEPTION);

$result = $client->batch()
    ->call('Util.Math.pow', [2, 1])
    ->call('Util.methodNotFound', [])
    ->batchExecute();
// $result = [2, MethodNotFoundException]

With exception mode server error present JsonRpcException object. Exception will not throw for saving other units of result., (*14)

The example with personal custom headers in request:, (*15)

<?php

use PhpJsonRpc\Client;
use PhpJsonRpc\Common\Interceptor\Container;
use PhpJsonRpc\Common\Interceptor\Interceptor;

$client = new Client('http://localhost');

$client->getTransport()->onPreRequest()
   ->add(Interceptor::createWith(function (Container $container) {
        // Get transport from container
        $transport = $container->first();

        // Add required headers
        $transport->addHeaders([
            "Origin: " . $_SERVER['HTTP_HOST'],
        ]);

        // Now we MUST return container for next chain
        return new Container($transport, $container->last());
    }));

$result = $client->call('Math.pow', [2, 3]); // $result = 8

Tests

$ ./vendor/bin/phpunit -c ./

The Versions

02/03 2018

dev-master

9999999-dev https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

02/03 2018

v1.2.1

1.2.1.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

24/01 2018

1.2.0

1.2.0.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

28/03 2017

v1.1.5

1.1.5.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

28/03 2017

v1.1.4

1.1.4.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

11/03 2017

1.1.3

1.1.3.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

16/02 2017

1.1.2

1.1.2.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

10/01 2017

v1.1.1

1.1.1.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

16/12 2016

v1.0.1

1.0.1.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

10/12 2016

v1.0.0

1.0.0.0 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2

10/12 2016

v1.0.0-RC1

1.0.0.0-RC1 https://github.com/vaderangry/PhpJsonRpc

Flexible JSON-RPC2 server/client implementation for PHP7

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

by Pakhomov D.

json rpc json-rpc2