1. Go to this page and download the library: Download rikudou/json-rpc-bundle 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/ */
rikudou / json-rpc-bundle example snippets
use Rikudou\JsonRpcBundle\Response\JsonRpcResponse;
use Rikudou\JsonRpcBundle\Request\JsonRpcRequest;
use Rikudou\JsonRpcBundle\Service\JsonRpcResponder;
use Symfony\Component\Routing\Annotation\Route;
final class MyController
{
#[Route('/jsonrpc')]
public function myHandler(JsonRpcRequest $request, JsonRpcResponder $responder): JsonRpcResponse
{
return $responder->respond($request);
}
}
use Rikudou\JsonRpcBundle\Attribute\JsonRpcMethod;
#[JsonRpcMethod('myMethodName')]
final class MyMethod
{
public function __invoke(): string
{
return 'some-response';
}
}
use Rikudou\JsonRpcBundle\Attribute\JsonRpcMethod;
#[JsonRpcMethod('myMethodName')]
final class MyMethod
{
public function __invoke(string $parameter1, string $parameter2): string
{
return $parameter1 . $parameter2;
}
}
use Rikudou\JsonRpcBundle\Attribute\JsonRpcMethod;
#[JsonRpcMethod('myMethodName')]
final class MyMethod
{
public function __invoke(string $parameter1, string $parameter2, ...$allOtherParameters): string
{
return $parameter1 . $parameter2 . json_encode($allOtherParameters);
}
}
use Rikudou\JsonRpcBundle\JsonRpc\JsonRpcMethod;
use Rikudou\JsonRpcBundle\Request\JsonRpcRequestParams;
final class MyMethod implements JsonRpcMethod
{
public function getMethodName() : string
{
return 'myMethod';
}
public function execute(JsonRpcRequestParams $params) : int|string|null|float|JsonSerializable|array|stdClass
{
return 'some-response';
}
}
use Rikudou\JsonRpcBundle\JsonRpc\JsonRpcMethod;
use Rikudou\JsonRpcBundle\Request\JsonRpcRequestParams;
final class MyMethod implements JsonRpcMethod
{
public function getMethodName() : string
{
return 'myMethod';
}
public function execute(JsonRpcRequestParams $params) : int|string|null|float|JsonSerializable|array|stdClass
{
if (!$params->hasParams()) {
throw new RuntimeException('There are no parameters!');
}
if (!isset($params['myParam'])) {
throw new RuntimeException('The parameter "myParam" is missing!');
}
$allParameters = $params->getParams();
return $params['myParam'] . json_encode($allParameters);
}
}
use Rikudou\JsonRpcBundle\Service\JsonRpcRequestParser;
use Rikudou\JsonRpcBundle\Request\JsonRpcRequest;
use Symfony\Component\HttpFoundation\RequestStack;
final class MyService
{
public function __construct(private JsonRpcRequestParser $parser, private RequestStack $requestStack)
{
}
public function getRequest(): JsonRpcRequest
{
// if you don't provide a Symfony\Component\HttpFoundation\Request parameter, the current request is used
$request = $this->parser->parse();
// or you can specify the request yourself
$request = $this->parser->parse($this->requestStack->getCurrentRequest());
return $this->parser->parse();
}
}
use Symfony\Component\HttpFoundation\JsonResponse;
use Rikudou\JsonRpcBundle\Request\JsonRpcRequest;
use Rikudou\JsonRpcBundle\Service\JsonRpcResponder;
use Symfony\Component\Routing\Annotation\Route;
use Rikudou\JsonRpcBundle\Response\JsonRpcSingleResponse;
use Rikudou\JsonRpcBundle\Response\JsonRpcBatchResponse;
final class JsonRpcController
{
#[Route('/jsonrpc')]
public function jsonRpc(JsonRpcRequest $request, JsonRpcResponder $responder): JsonResponse
{
$response = $responder->respond($request);
$rawData = $response->getJson();
if ($response instanceof JsonRpcSingleResponse) {
// do something with $rawData
} elseif ($response instanceof JsonRpcBatchResponse) {
// do something with $rawData
}
return new JsonResponse($rawData, $response->getStatusCode());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.