1. Go to this page and download the library: Download jdolieslager/jsonrpc 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/ */
jdolieslager / jsonrpc example snippets
class Test
{
public function hello($name)
{
return "Hello {$name}!";
}
}
$server = new Jdolieslager\JsonRpc\Server(true);
$server->registerHandler('Test'); // Second argument you can define a namespace
// Methods are than seperated with <namespace>.<orignal_method_name>
$server->printResponseForRawRequest(file_get_contents('php://input'));
$client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine');
$response = $client->sendSimpleRequest('hello', array('name' => 'World'));
if ($response->hasError()) {
$error = $response->getError();
throw new \Exception($error->getMessage(), $error->getCode());
} else {
echo $response->getResult();
}
$request = new Jdolieslager\JsonRpc\Entity\Request();
$request->setId(1);
$request->setJsonrpc(Jdolieslager\JsonRpc\Client::VERSION_1);
$request->setMethod('hello');
$request->addParam('World', 'name');
$client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine');
$response = $client->sendSingleRequest($request);
if ($response->hasError()) {
$error = $response->getError();
throw new \Exception($error->getMessage(), $error->getCode());
} else {
echo $response->getResult();
}
$client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine');
$client->sendNotification('hello', array('name' => 'World'));
use Jdolieslager\JsonRpc\Entity\Response;
// Create objects
$client = new Jdolieslager\JsonRpc\Client('http://jsonrpc.mine');
$collection = new Jdolieslager\JsonRpc\Collection\Request();
$request = new Jdolieslager\JsonRpc\Entity\Request();
// Create request one
$request->setId(1);
$request->setMethod('hello');
$request->addParam('World', 'name');
$collection->addRequest($request, function(Response $response) {
if ($response->hasError()) {
$error = $response->getError();
throw new \Exception($error->getMessage(), $error->getCode());
} else {
echo $response->getResult();
}
});
// Create request two
$request = new Jdolieslager\JsonRpc\Entity\Request();
$request->setId(2);
$request->setMethod('hello');
$request->addParam('Jesper', 'name');
$collection->addRequest($request, function(Response $response) {
if ($response->hasError()) {
$error = $response->getError();
throw new \Exception($error->getMessage(), $error->getCode());
} else {
echo $response->getResult();
}
});
// Responses holds all the responses
// Index is the ID number
$responses = $client->sendRequest($collection);
class Test
{
public function hello($name)
{
return 'Hello ' . ucfirst($name) . '!';
}
}
$layer = new Jdolieslager\JsonRpc\ProtocolLayer\ServerEncryption(array(
'key' => 'my secret pass here'
));
$server = new Jdolieslager\JsonRpc\Server();
$server->registerHandler('Test');
$server->addProtocolLayer($layer, Jdolieslager\JsonRpc\Server::LAYER_PLACEMENT_BOTTOM);
$server->printResponseForRawRequest(file_get_contents('php://input'));