PHPackages                             mihasicehcek/php\_json\_rpc\_2\_server - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mihasicehcek/php\_json\_rpc\_2\_server

ActiveLibrary

mihasicehcek/php\_json\_rpc\_2\_server
======================================

php realisation of JSON RPC 2 Specification

v1.0.4(8y ago)05.7kMITPHP

Since Feb 7Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mihasicehcek/php_json_rpc_2_server)[ Packagist](https://packagist.org/packages/mihasicehcek/php_json_rpc_2_server)[ RSS](/packages/mihasicehcek-php-json-rpc-2-server/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (6)Used By (0)

PHP JsonRPC2 Server
===================

[](#php-jsonrpc2-server)

Description
-----------

[](#description)

[![Latest Stable Version](https://camo.githubusercontent.com/1c32484cb932a40c2945311f0fbe177d0088936d6ee6b69e7ba073fcdc70cabd/68747470733a2f2f706f7365722e707567782e6f72672f6d696861736963656863656b2f7068705f6a736f6e5f7270635f325f7365727665722f76657273696f6e)](https://packagist.org/packages/mihasicehcek/php_json_rpc_2_server)[![Total Downloads](https://camo.githubusercontent.com/d581fe8155e8e81e43592e6482ca65a4f46eac947d1ae8e9b139ee549255f1ae/68747470733a2f2f706f7365722e707567782e6f72672f6d696861736963656863656b2f7068705f6a736f6e5f7270635f325f7365727665722f646f776e6c6f616473)](https://packagist.org/packages/mihasicehcek/php_json_rpc_2_server)[![License](https://camo.githubusercontent.com/fd2f58dbcff6e54fceb1e1d955a00e54407e8acbaa7754c281d4d29a4e0f01dd/68747470733a2f2f706f7365722e707567782e6f72672f6d696861736963656863656b2f7068705f6a736f6e5f7270635f325f7365727665722f6c6963656e7365)](https://packagist.org/packages/mihasicehcek/php_json_rpc_2_server)

It is php implementation of the [JSON-RPC 2.0](http://www.jsonrpc.org/specification "Specification") server

Working principle is very simple

1. Create an instance of the class \\PhpJsonRpc2\\JsonRpcServer
2. In method setRequest pass instance or array of \\PhpJsonRpc2\\Request
3. Using method getResponse getting instance or array of instances \\PhpJsonRpc2\\Response

There is also a \\PhpJsonRpc2\\ICallStrategy interface that can configure \\PhpJsonRpc2\\JsonRpcServer for changing strategy of calling procedure. By default \\PhpJsonRpc2\\JsonRpcServer is configured by strategy \\PhpJsonRpc2\\ClassMethodCallStrategy, that divide request parameter "method"(for example Calculator.sum) by dot and create "Calculator" instance and call its method "sum".

Installation using composer

```
$ composer require composer require mihasicehcek/php_json_rpc_2_server

```

\##Examples

\###Simple Example

```
//simulating a request
$json = json_encode([
    "jsonrpc" => "2.0",
    "method" => "Calculator.sum",
    "params" => [ "a" => 10, "b" => 15],
    "id" => 1
]);

//Creating instance of \PhpJsonRpc2\JsonRpcServer
$jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer();

//Passing json string
$jsonRpcServer->setRequestAsJson($json);

//geting Response
$response = $jsonRpcServer->getResponse();

echo json_encode($response); //{ jsonrpc : "2.0", result : 2, id : 1}
```

\###Expamle with pre-conversion json string into object

You can convert json into instance or instances array before passing it into server object. That can be implemented using static method\\PhpJsonRpc2\\Request::requestFactory($json). But in this case you need to look after handling \\PhpJsonRpc2\\ParseErrorException, that can be throwed in case of invalid json parsing

```
$json = json_encode([
    "jsonrpc" => "2.0",
    "method" => "Calculator.sum",
    "params" => [10, 15],
    "id" => 1
]);

//Create request from json
$request = Request::requestFactory($json);

$jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer();

//pass json into setRequest method
$jsonRpcServer->setRequest($request);

$response = $jsonRpcServer->getResponse();

echo json_encode($response); //{ jsonrpc : "2.0", result : 2, id : 1}
```

\##Notification

[Notification](http://www.jsonrpc.org/specification#notification "Specification") if passed, the response is returned null.

```
$json = json_encode([
    "jsonrpc" => "2.0",
    "method" => "Calculator.saveSum",
    "params" => [15],
    "id" => null
]);

$jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer();

$jsonRpcServer->setRequestAsJson($json);

$response = $jsonRpcServer->getResponse(); //null
```

\##Batch

See [Batch](http://www.jsonrpc.org/specification#batch "Specification")

```
$json = json_encode([
    [
        "jsonrpc" => "2.0",
        "method" => "Calculator.sum",
        "params" => [15, 15],
        "id" => 1
    ],
    [
        "jsonrpc" => "2.0",
        "method" => "Calculator.sum",
        "params" => [10, 50],
        "id" => 2
    ]
]);

$jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer();

$jsonRpcServer->setRequestAsJson($json);

$response = $jsonRpcServer->getResponse(); //null

json_encode($response) //[{ jsonrpc : "2.0", result : 15, id : 1},{ jsonrpc : "2.0", result : 60, id : 2}]
```

\##Custom strategy call

You can define your own methods strategy call For example we want to provide "method" not as class.method, but invoke some simple functions

```
//Create our implementation if \PhpJsonRpc2\ICallStrategy interface(For simplicity, we imagine that we pass only the positional parameters)
class SimpleMethodCallStrategy implements ICallStrategy
{

    /**
    *@param $method string method name(define in "method" parameter of json object)
    *@param $params array It can be as indexing and associative array
    */
    public function call($method, $params)
    {
        $function = new \ReflectionFunction($method);

        return $function->invokeArgs($params);
    }

}

$request = [
    "jsonrpc" => "2.0",
    "method" => "sum",
    "params" => [10, 15],
    "id" => 1
];

$json = json_encode($request);

$jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer();
//configure instance of JsonRpcServer by insatnce of SimpleMethodCallStrategy
$jsonRpcServer->setCallStrategy(new SimpleMethodCallStrategy());

$jsonRpcServer->setRequestAsJson($json);

$response = $jsonRpcServer->getResponse();

echo json_encode($response); //{ jsonrpc : "2.0", result : 2, id : 1}
```

\##Exceptions

All exceptions is subtype of base \\PhpJsonRpc2\\BaseJsonRpcException. Exceptions not throwing, but its instance returned in response.

```
$json = json_encode('{"jsonrpc": "2.0", "method": "foobar, "params": "bar", "baz]');

$jsonRpcServer = new \PhpJsonRpc2\JsonRpcServer();

$jsonRpcServer->setRequestAsJson($json);

$response = $jsonRpcServer->getResponse();

echo json_encode($response); //{ jsonrpc : "2.0", error : { code : -32700, message : "Parse error"}, id : null}
```

If you want to throw an exception in procedures, you need to throw instance of \\PhpJsonRpc2\\InternalException or its subtype

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~55 days

Total

5

Last Release

3162d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/61639a0f0afc160acbf538c041d6411f1a18a3ea10193812e29d49354c3037a1?d=identicon)[mihasicehcek](/maintainers/mihasicehcek)

---

Top Contributors

[![mihasicehcek](https://avatars.githubusercontent.com/u/10881356?v=4)](https://github.com/mihasicehcek "mihasicehcek (8 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mihasicehcek-php-json-rpc-2-server/health.svg)

```
[![Health](https://phpackages.com/badges/mihasicehcek-php-json-rpc-2-server/health.svg)](https://phpackages.com/packages/mihasicehcek-php-json-rpc-2-server)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
