PHPackages                             barell/json-rpc-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. [API Development](/categories/api)
4. /
5. barell/json-rpc-server

ActiveLibrary[API Development](/categories/api)

barell/json-rpc-server
======================

JSON-RPC 2.0 Server for PHP5/7

v1.0.4(9y ago)029MITPHPPHP &gt;=5.5.0

Since Mar 30Pushed 9y ago1 watchersCompare

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

READMEChangelog (7)DependenciesVersions (8)Used By (0)

[![Build Status](https://camo.githubusercontent.com/ddd38c820241095a24613672b66b3bafcf15e9c74dd6108a4497a21cc35e229d/68747470733a2f2f7472617669732d63692e6f72672f626172656c6c2f6a736f6e2d7270632d7365727665722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/barell/json-rpc-server)

JSON-RPC Server
===============

[](#json-rpc-server)

Welcome to JSON-RPC Server library written in PHP, compatible with version 5.5 and higher and fully supporting the [JSON-RPC 2.0 specification](http://www.jsonrpc.org/specification). It can work with incoming connections over HTTP using POST method but can be easily extended to read from any input.

Current version: **1.0.4**

Release date: **2017-05-17**

License: **MIT**

Installation
------------

[](#installation)

To start using in your project, run composer command:

```
composer require barell/json-rpc-server

```

Basic Usage
-----------

[](#basic-usage)

Below the example of how to expose a *hello* method of the class *ExampleService*:

```
class ExampleService
{
    public function hello($name)
    {
        return 'Hello ' . $name . '!';
    }
}

use JsonRpcServer\Server;

// Create server instance with default options
$server = Server::createDefault();

// Add hello method from ExampleService class
$server->addMethod('hello', 'ExampleService');

// Alternatively add get method from UserService class but expose it as getUser
$server->addMethod('getUser', 'UserService', 'get');

// Finally handle and output the result
$server->handle()->output();
```

The method *hello* is now externally available to call over HTTP POST using request like:

```
{
  "jsonrpc": "2.0",
  "method": "hello",
  "params": {
    "name": "John"
  },
  "id": 1
}
```

The output will be:

```
{
  "jsonrpc": "2.0",
  "result": "Hello John!",
  "id": 1
}
```

Advanced Topics
---------------

[](#advanced-topics)

### Custom Input Data Handler

[](#custom-input-data-handler)

This server accepts all incoming connections from HTTP POST by default but there is an easy way of implementing your own data handling. In case you would like to use other HTTP method or get incoming data from any other source, implement your handler using *IHandler* interface:

```
use JsonRpcServer\IHandler;

class MyHandler implements IHandler
{
    public function getData()
    {
        // Implement your own source of incoming data here...
    }
}
```

Then when you initialize the server:

```
// Create server
$server = Server::createDefault();

// Tell server to use your handler
$server->setHandler(new MyHandler());
```

### Custom JSON serialization

[](#custom-json-serialization)

All json encoding and decoding inside the server are based on the PHP built in *json\_encode* and *json\_decode* functions which are enough for most of the uses. Although, sometimes there is a need of custom json handling, so you can convert PHP objects into json representation. In this case you can implement your own Codec, using *ICodec* interface:

```
use JsonRpcServer\ICodec;

class MyCodec implements ICodec
{
    public function decode($data)
    {
        // decode json rpc object using your own implementation
    }

    public function encode($data)
    {
        // encode PHP array response into json string
    }
}
```

Then when you initialize the server:

```
// Create server
$server = Server::createDefault();

// Tell server to use your handler
$server->setCodec(new MyCodec());
```

### Custom Errors

[](#custom-errors)

By default any exceptions returned within your exposed method will be shown as internal server error. To return customer error message and code, please throw an exception using *JsonRpcUserException* class any derivative of it. See example:

```
class ExampleService
{
    public function divide($a, $b)
    {
        if ($b == 0) {
            throw new JsonRpcUserException('Division by zero is not allowed', 1234);
        }

        return $a / $b;
    }
}
```

Whenever zero will be passed as the second parameter, the server output will be:

```
{
  "jsonrpc": "2.0",
  "error": {
    "code": 1234,
    "message": "Division by zero is not allowed"
  },
  "id": null
}
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity63

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 ~9 days

Total

6

Last Release

3285d ago

Major Versions

v0.9.0 → v1.0.02017-04-02

### Community

---

Top Contributors

[![barell](https://avatars.githubusercontent.com/u/1502894?v=4)](https://github.com/barell "barell (37 commits)")

---

Tags

json-rpcphpserver

### Embed Badge

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

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

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

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