Basic UsageΒΆ
Following a quick and dirty example of lib basic usage, without a framework that mediates RPC requests.
Note
For more detailed informations, please see Using the RPC Server and Creating methods pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?php
use \Comodojo\RpcServer\RpcServer;
use \Comodojo\RpcServer\RpcMethod;
use \Exception;
// get the raw request payload (using, for example, an HTTP::POST)
$payload = file_get_contents('php://input');
try {
// create a RpcServer instance (i.e. JSON)
$server = new RpcServer(RpcServer::JSONRPC);
// create a method (using a lambda functions)
$method = RpcMethod::create("example.sum", function($params) {
$a = $params->get('a');
$b = $params->get('b');
return intval($a) + intval($b);
})
->setDescription("Sum two integers")
->setReturnType('int')
->addParameter('int','a')
->addParameter('int','b');
// register newly created method into server
$server->getMethods()->add($method);
// set the payload
$server->setPayload($request);
// serve the request
$result = $server->serve();
} catch (Exception $e) {
/* something did not work :( */
throw $e;
}
echo $result;
|