PHP code example of rambler-digital-solutions / php-json-rpc

1. Go to this page and download the library: Download rambler-digital-solutions/php-json-rpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rambler-digital-solutions / php-json-rpc example snippets




use JsonRPC\Server;

$server = new Server;

// Procedures registration
$server->register('addition', function ($a, $b) {
    return $a + $b;
});

$server->register('random', function ($start, $end) {
    return mt_rand($start, $end);
});

// Return the response to the client
echo $server->execute();




use JsonRPC\Server;

class Api
{
    public function doSomething($arg1, $arg2 = 3)
    {
        return $arg1 + $arg2;
    }
}

$server = new Server;

// Bind the method Api::doSomething() to the procedure myProcedure
$server->bind('myProcedure', 'Api', 'doSomething');

// Use a class instance instead of the class name
$server->bind('mySecondProcedure', new Api, 'doSomething');

// The procedure and the method are the same
$server->bind('doSomething', 'Api');

// Attach the class, client will be able to call directly Api::doSomething()
$server->attach(new Api);

echo $server->execute();




use JsonRPC\Server;
use JsonRPC\AuthenticationFailure;

class Api
{
    public function beforeProcedure($username, $password, $class, $method)
    {
        if ($login_condition_failed) {
            throw new AuthenticationFailure('Wrong credentials!');
        }
    }

    public function addition($a, $b)
    {
        return $a + $b;
    }
}

$server = new Server;
$server->authentication(['myuser' => 'mypassword']);

// Register the before callback
$server->before('beforeProcedure');

$server->attach(new Api);

echo $server->execute();




use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$result = $client->execute('addition', [3, 5]);

var_dump($result);



use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$result = $client->execute('random', ['end' => 10, 'start' => 1]);

var_dump($result);



use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$result = $client->random(50, 100);

var_dump($result);

$result = $client->random(['end' => 10, 'start' => 1]);



use JsonRPC\Client;

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

$results = $client->batch()
                  ->foo(['arg1' => 'bar'])
                  ->random(1, 100)
                  ->add(4, 3)
                  ->execute('add', [2, 5])
                  ->send();

print_r($results);



use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$client->debug = true;



use JsonRPC\Server;

$server = new Server;

// IP client restrictions
$server->allowHosts(['192.168.0.1', '127.0.0.1']);

// Procedures registration

[...]

// Return the response to the client
echo $server->execute();



use JsonRPC\Server;

$server = new Server;

// List of users to allow
$server->authentication(['user1' => 'password1', 'user2' => 'password2']);

// Procedures registration

[...]

// Return the response to the client
echo $server->execute();



use JsonRPC\Client;

$client = new Client('http://localhost/server.php');
$client->authentication('user1', 'password1');


use JsonRPC\Server;

$server = new Server;
$server->setAuthenticationHeader('X-Authentication');
$server->authentication(['myusername' => 'mypassword']);



use JsonRPC\Server;
class MyException extends RuntimeException {};

$server = new Server;

// Exceptions that should be relayed to the client, if they occur
$server->attachException('MyException');

// Procedures registration

[...]

// Return the response to the client
echo $server->execute();
bash
composer