1. Go to this page and download the library: Download vaderangry/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/ */
vaderangry / php-json-rpc example snippets
use PhpJsonRpc\Server;
// Class for which provide JSON-RPC2 API:
class Math
{
public function pow(float $base, int $exp): float
{
return pow($base, $exp);
}
}
$server = new Server();
$response = $server
->addHandler(new Math())
->execute();
echo $response;
use PhpJsonRpc\Server;
use PhpJsonRpc\Server\MapperInterface;
// Define custom mapper
class Mapper implements MapperInterface
{
public function getClassAndMethod(string $requestedMethod): array
{
// Keys of array presents requested method
$map = [
'pow' => [Math::class, 'pow'],
];
if (array_key_exists($requestedMethod, $map)) {
return $map[$requestedMethod];
}
return ['', ''];
}
}
$server = new Server();
// Register new mapper
$server->setMapper(new Mapper());
// Register handler and run server
$response = $server->addHandler(new Math())->execute();
echo $response;
use PhpJsonRpc\Client;
use PhpJsonRpc\Common\Interceptor\Container;
use PhpJsonRpc\Common\Interceptor\Interceptor;
$client = new Client('http://localhost');
$client->getTransport()->onPreRequest()
->add(Interceptor::createWith(function (Container $container) {
// Get transport from container
$transport = $container->first();
// Add
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.