1. Go to this page and download the library: Download suscitar/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.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
useJsonRPC\Server;
classApi{
publicfunctiondoSomething($arg1, $arg2 = 3){
return $arg1 + $arg2;
}
}
$server = new Server();
$procedureHandler = $server->getProcedureHandler();
// Bind the method Api::doSomething() to the procedure myProcedure
$procedureHandler->withClassAndMethod('myProcedure', 'Api', 'doSomething');
// Use a class instance instead of the class name
$procedureHandler->withClassAndMethod('mySecondProcedure', new Api, 'doSomething');
// The procedure and the method are the same
$procedureHandler->withClassAndMethod('doSomething', 'Api');
// Attach the class, the client will be able to call directly Api::doSomething()
$procedureHandler->withObject(new Api());
echo $server->execute();
useJsonRPC\Client;
$client = new Client('http://localhost/server.php');
$client->getHttpClient()->withDebug();
useJsonRPC\Server;
$server = new Server;
// IP client restrictions
$server->allowHosts(['192.168.0.1', '127.0.0.1']);
[...]
// Return the response to the clientecho $server->execute();
useJsonRPC\Server;
$server = new Server;
// List of users to allow
$server->authentication(['user1' => 'password1', 'user2' => 'password2']);
[...]
// Return the response to the clientecho $server->execute();
useJsonRPC\Client;
$client = new Client('http://localhost/server.php');
$client->getHttpClient()
->withUsername('Foo')
->withPassword('Bar');
useJsonRPC\Server;
$server = new Server();
$server->setAuthenticationHeader('X-Authentication');
$server->authentication(['myusername' => 'mypassword']);
useJsonRPC\Server;
classMyException1extendsException{};
classMyException2extendsException{};
$server = new Server();
// Exceptions that should NOT be relayed to the client, if they occurs
$server
->withLocalException('MyException1')
->withLocalException('MyException2')
;
[...]
echo $server->execute();