2017 © Pedro Peláez
 

library json-rpc

JSON-RPC 2.0 server in PHP

image

procurios/json-rpc

JSON-RPC 2.0 server in PHP

  • Thursday, January 7, 2016
  • by tacovandenbroek
  • Repository
  • 4 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

JSON-RPC 2.0 Server supporting PSR-7

Build Status Coverage Status, (*1)

Server

The server is a complete implementation of the JSON-RPC 2.0 specification. The server will expose public methods of an object or a static class which can be optionally limited using an interface or specific parent class., (*2)

To encourage interface segregation there is no support for other methods like closures or global functions., (*3)

Features

  • Full specification including notifications, both parameters by name and by position and batch requests
  • Default values are used for skipped arguments
  • Variadic arguments are supported
  • PSR-7 compatible: This server can directly handle implementations of Psr\Http\Message\ServerRequestInterface, returning an implementation of Psr\Http\Message\ResponseInterface, as defined in PSR-7

Requirements

PHP >= 8.0, (*4)

Example

Subject classes

MyInterface

<?php
interface MyInterface
{
    public function foo();
}

MySubjectClass

<?php
class MySubjectClass implements MyInterface
{
    public function foo()
    {
        return 'foo';
    }

    public function bar()
    {
        return 'bar';
    }
}

Handle request directly

<?php
use Procurios\Json\JsonRpc\Server;
use Procurios\Json\JsonRpc\Request\Request;

$requestData = json_decode(file_get_contents('php://input'), true);
$Request = Request::fromArray($requestData);

$Server = new Server(new MySubjectClass);
$Response = $Server->handleRequest($Request);

header('Content-Type: application/json');
die($Response->asString());

Handle PSR-7 ServerRequestInterface

<?php
use Procurios\Json\JsonRpc\Server;

$Server = new Server(new MySubjectClass);

// Use the current Psr\Http\Message\ServerRequestInterface implementation in your application
$Request = MyRequestSource::getRequest();

// Create an empty implementation of Psr\Http\Message\ResponseInterface
$BaseResponse = MyResponseFactory::createResponse();

$Response = $Server->handleServerRequest($Request, $BaseResponse);

MyResponseEmitter::emit($Response);

Limit subject to an interface

<?php
use Procurios\Json\JsonRpc\Server;

$Server = new Server(new MySubjectClass, MyInterface::class);

// Only the method foo will be available in this server, since bar is not part of the interface

The Versions

07/01 2016

dev-master

9999999-dev https://github.com/procurios/JsonRpc

JSON-RPC 2.0 server in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

psr psr-7 json server json-rpc rpc

05/01 2016

v1.0-beta

1.0.0.0-beta https://github.com/procurios/JsonRpc

JSON-RPC 2.0 server in PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

psr psr-7 json server json-rpc rpc