1. Go to this page and download the library: Download superconductor/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/ */
use Superconductor\Rpc\Support\Facades\RPC;
RPC::method('math/add', MathController::class);
RPC::method('math/subtract', MathController::class.'@subtract');
namespace App\Rpc\Procedures;
use Superconductor\Rpc\Rpc\Procedures\RpcProcedure;
use Superconductor\Rpc\DTO\Messages\Outgoing\RpcResult;
use Superconductor\Rpc\DTO\Messages\Outgoing\RpcError;
class MathController extends RpcProcedure
{
public function handle(array $params): RpcResult|RpcError
{
$result = $params['a'] + $params['b'];
return new RpcResult(request()->get('id'), $result);
}
public function subtract(array $params): RpcResult|RpcError
{
$result = $params['a'] - $params['b'];
return new RpcResult(request()->get('id'), $result);
}
}
use Superconductor\Rpc\DTO\Messages\RpcMessage;
use Superconductor\Rpc\Support\Facades\RPC;
// Parse JSON-RPC request
$jsonrpc = '{"jsonrpc": "2.0", "method": "math/add", "params": {"a": 5, "b": 3}, "id": 1}';
$request = RpcMessage::fromJsonRpc($jsonrpc);
// Execute the procedure
$response = RPC::call($request);
// Send response back to client
return response()->json($response->toJsonRpc());
use Superconductor\Rpc\DTO\Messages\Incoming\RpcRequest;
use Superconductor\Rpc\Support\Attributes\UsesRpcRequest;
#[UsesRpcRequest(CalculateRequest::class)]
class CalculatorController extends RpcProcedure
{
public function calculate(CalculateRequest $request): RpcResult|RpcError
{
$result = match($request->operation) {
'add' => $request->a + $request->b,
'subtract' => $request->a - $request->b,
'multiply' => $request->a * $request->b,
'divide' => $request->b !== 0 ? $request->a / $request->b : throw new DivisionByZeroError(),
};
return new RpcResult($request->id, $result);
}
}
class CalculateRequest extends RpcRequest
{
public function __construct(
int $id,
public readonly float $a,
public readonly float $b,
public readonly string $operation,
) {
parent::__construct($id, 'calculate', [
'a' => $a,
'b' => $b,
'operation' => $operation,
]);
}
public static function fromRpcRequest(RpcRequest $request): static
{
return new self(
$request->id,
...$request->params
);
}
}
use Superconductor\Rpc\DTO\Messages\Incoming\RpcNotification;
// Parse notification (no ID, no response expected)
$jsonrpc = '{"jsonrpc": "2.0", "method": "user/logActivity", "params": {"action": "login"}}';
$notification = RpcMessage::fromJsonRpc($jsonrpc);
// Fire and forget - no response
RPC::notify($notification);
public function divide(array $params): RpcResult|RpcError
{
if ($params['b'] === 0) {
return $this->returnError(
request()->get('id'),
RPCErrorCode::INVALID_PARAMS,
"Division by zero is not allowed"
);
}
$result = $params['a'] / $params['b'];
return new RpcResult(request()->get('id'), $result);
}
enum RPCErrorCode: int
{
case PARSE_ERROR = -32700;
case INVALID_REQUEST = -32600;
case METHOD_NOT_FOUND = -32601;
case INVALID_PARAMS = -32602;
case INTERNAL_ERROR = -32603;
case SERVER_ERROR = -32000;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.