veejay/jsonrpc

JSONRPC server and client

Maintainers

Package info

github.com/Veejayspb/jsonrpc

pkg:composer/veejay/jsonrpc

Statistics

Installs: 15

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

2.0.1 2026-03-24 13:53 UTC

This package is auto-updated.

Last update: 2026-03-24 13:55:07 UTC


README

Jsonrpc 2.0 for PHP over HTTP(S).

License: MIT

Examples

Server

At first, you have to extend Veejay\Jsonrpc\Api class and add required methods:

<?php

use Veejay\Jsonrpc\Api;
use Veejay\Jsonrpc\Exception\InvalidParamsException;

class MyApi extends Api
{
    protected const TOKEN = 'qwerty';

    public function __call(string $name, array $arguments): mixed
    {
        if (!isset($arguments['token']) || self::TOKEN !== $arguments['token']) {
            throw new InvalidParamsException('Invalid token');
        }

        unset($arguments['token']);

        if (method_exists($this, $name)) {
            return call_user_func_array([$this, $name], $arguments);
        }

        return parent::__call($name, $arguments);
    }

    public function publicMethod(int $id): string
    {
        return 'Your id: ' . $id;
    }
    
    protected function protectedMethod()
    {
        return 'Protected';
    }
}

Then run Server with the following code:

<?php

use Veejay\Jsonrpc\Server;

$server = new Server(new MyApi);
echo $response = $server->run();

Client

<?php

use Veejay\Jsonrpc\Client;

$client = new Client('https://jsonrpc/server/address');

$query = $client->query('publicMethod', ['id' => 1]); // Your id: 1
$query = $client->query('protectedMethod', ['token' => 'qwerty']); // Protected
$client->notify('publicMethod');

$client->send();

You will receive the response from server in $query variables.

Requirements

  • PHP 8.0+

Installation

composer require "veejay/jsonrpc"