PHP code example of jalle19 / simple-json-rpc-client
1. Go to this page and download the library: Download jalle19/simple-json-rpc-client 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/ */
jalle19 / simple-json-rpc-client example snippets
use SimpleJsonRpcClient\Client\HttpPostClient as Client;
use SimpleJsonRpcClient\Request\Request;
use SimpleJsonRpcClient\Exception\BaseException;
use SimpleJsonRpcClient\Response\Response;
// Initialize the client. Credentials are optional.
$client = new Client('localhost', 'username', 'password');
try
{
// Send a request without parameters. The "id" will be added automatically unless supplied.
// Request objects return their JSON representation when treated as strings.
$request = new Request('method');
$response = $client->sendRequest($request);
// Send a request with parameters specified as an array
$request = new Request('method', array('param1'=>'value1'));
$response = $client->sendRequest($request);
// Send a request with parameters specified as an object
$params = new stdClass();
$params->param1 = 'value1';
$request = new Request('method', $params);
$response = $client->sendRequest($request);
// Send a parameter-less request with specific "id"
$request = new Request('method', null, 2);
$response = $client->sendRequest($request);
}
catch (BaseException $e)
{
echo $e->getMessage();
}
use SimpleJsonRpcClient\Client\HttpPostClient as Client;
use SimpleJsonRpcClient\Request\Notification;
use SimpleJsonRpcClient\Exception\BaseException;
$client = new Client('localhost', 'username', 'password');
try
{
$request = new Notification('notification');
$client->sendNotification($request);
}
catch (BaseException $e)
{
echo $e->getMessage();
}
use SimpleJsonRpcClient\Client\HttpPostClient as Client;
use SimpleJsonRpcClient\Request;
use SimpleJsonRpcClient\Exception\BaseException;
$client = new Client('localhost', 'username', 'password');
try
{
$request = new Request\BatchRequest(array(
new Request\Request('method'),
new Request\Notification('anotherMethod'),
new Request\Request('yetAnotherMethod', null, 3)
));
$batchResponse = $client->sendBatchRequest($request);
// Retrieve all response objects
$responses = $batchResponse->getResponses();
// Get specific response
$response = $batchResponse->getResponse(3);
}
catch (BaseException $e)
{
echo $e->getMessage();
}
use SimpleJsonRpcClient\Client\HttpPostClient as Client;
use SimpleJsonRpcClient\Request;
use SimpleJsonRpcClient\Exception;
use SimpleJsonRpcClient\Response;
$client = new Client('localhost', 'username', 'password');
try
{
$request = new Request\Request('method', array('param1'=>'value1'));
$response = $client->sendRequest($request);
}
catch (ClientException $ce) {
// The client failed to execute the request
}
catch (InvalidResponseException $e)
{
// The response was invalid, e.g. it could not be parsed or it is not standard-compliant
}
catch (ResponseErrorException $re) {
// The request itself was successful but the JSON-RPC response indicates an error.
// A subclass of ResponseErrorException is thrown for pre-defined errors (see http://www.jsonrpc.org/specification#error_object)
}
catch (Exception $e) {
// Anything else, usually InvalidArgumentException
}