PHP code example of alanbem / josser

1. Go to this page and download the library: Download alanbem/josser 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/ */

    

alanbem / josser example snippets




namespace Josser;

use GuzzleHttp\Client;
use Josser\Client\Transport\Guzzle6Transport as HttpTransport; // there is also guzzle 5 implementation available
use Josser\Protocol\JsonRpc1;

$guzzle    = new Client(['base_uri' => 'http://user:[email protected]/math:8888']); // http client
$transport = new HttpTransport($guzzle); // RPC over http
$protocol  = new JsonRpc1; // lets use JSON-RPC 1.0

$client = new Client($transport, $protocol);

// send a request
$sum = $client->request('sum', array(5, 4));

var_dump($sum); // int(9)



// instantiate client

$client->notify('logout');



namespace Josser;

use Josser\Exception\RpcFaultException;
use Josser\Exception\TransportFailureException;
use Josser\Exception\RequestResponseMismatchException;
use Josser\Exception\InvalidResponseException;
use Josser\Exception\InvalidRequestException;

// instantiate client

try {
    $client->request('method', array(1, "param2"));
} catch (RpcFaultException $e) {
    echo 'Ups! Remote server sent an error.';
} catch (TransportFailureException $e) {
    echo 'Josser did not send remote call.';
} catch (RequestResponseMismatchException $e) {
    echo "Response id does not match request id.";
} catch (InvalidResponseException $e) {
    echo "Response object is invalid due to protocol constraints.";
} catch(InvalidRequestException $e) {
    echo "Request is invalid due to protocol constraints.";
}              



namespace Josser;

use Josser\Client;
use Josser\Exception\JosserException;

// instantiate client

try {
    $client->request('method', array(1, "param2"));
} catch (JosserException $e) {
    echo 'Josser error occurred.';
}           



namespace Josser;

use Josser\Client;
use Josser\Client\Request\Request;
use Josser\Client\Request\Notification;

// instantiate transport and protocol

$client = new Client($transport, $protocol);
$client->request('math.divide', array(1, 2));
$client->notify('system.logout');

// code above is equivalent of

$request = new Request('math.divide', array(1, 2), 213123); // 213123 is a request identifier
$client->call($request);

$notification = new Notification('system.logout');
$client->call($notification);



namespace Josser;

use Josser\Client;
use Josser\Client\Request\Request;
use Josser\Client\Request\Notification;

// instantiate client

$request = new Request('math.sum', array(2, 2), 6564653);
$response = $client->call($request);

var_dump($response->getResult()); // int(4)



namespace MyProject\Request;

use Josser\Client\Request\Request;

class Divide extends Request
{
    public function __construct($divident, $divisor)
    {
        $requestId = uniqid(); // random request id
        parent::__construct('math.divide', array($divident, $divisor), $requestId);
    }
}