4.12. JsonRpcClient Class

Note: This class is not available with the PO_NO_NETWORK parse option.

The JsonRpcClient class provides easy access to JSON-RPC web services. This class inherits all public methods of the HTTPClient class. The inherited HTTPClient methods are not listed in this section, see the section on the HTTPClient class for more information on methods provided by the parent class. For low-level JSON-RPC functions, see the JSON-RPC functions in JSON Functions.

The JsonRpcClient class understands the following protocols in addition to the protocols supported by the HTTPClient class:

Table 4.481. JsonRpcClient Class Protocols

Protocol

Default Port

SSL?

Description

jsonrpc

80

No

Unencrypted JSON-RPC protocol over HTTP

jsonrpcs

443

Yes

JSON-RPC protocol over HTTP with SSL/TLS encryption


The JsonRpcClient supplies default values for HTTP headers as follows:

Table 4.482. JsonRpcClient Default, but Overridable Headers

Header

Default Value

Accept

application/json

Content-Type

application/json

User-Agent

Qore JSON-RPC Client v0.8.0

Connection

Keep-Alive


Table 4.483. JsonRpcClient Class Method Overview

Method

Except?

Description

JsonRpcClient::constructor(softbool $no_connect = False)

JsonRpcClient::constructor(hash $opts, bool $no_connect = False)

N

Creates the JsonRpcClient object based on the parameters passed.

JsonRpcClient::destructor()

N

Destroys the JsonRpcClient object and closes any open connections.

JsonRpcClient::copy()

Y

Copying objects of this class is not supported, an exception will be thrown.

JsonRpcClient::call(string $method, ...) returns any

Y

Calls a remote method using a variable number of arguments for the method arguments and returns the response as qore data structure.

JsonRpcClient::callArgs(string $method, any $args) returns any

Y

Calls a remote method using a single value after the method name for the method arguments and returns the response as qore data structure.

JsonRpcClient::callWithInfo(reference $info, string $method, ...) returns any

Y

Same as the JsonRpcClient::call() except the first argument must be an lvalue reference, which is used as an output variable, where information about the HTTP request and response is written.

JsonRpcClient::callArgsWithInfo(reference $info, string $method, any $args) returns any

Y

Same as the JsonRpcClient::callArgs() except the first argument must be an lvalue reference, which is used as an output variable, where information about the HTTP request and response is written.

JsonRpcClient::setEventQueue() returns nothing

JsonRpcClient::setEventQueue(Queue $queue) returns nothing

N

Sets a Queue object to receive HTTPClient and Socket events or clears the queue if no argument is passed.


4.12.1. JsonRpcClient::constructor()

Synopsis

Creates the JsonRpcClient object based on the parameters passed and by default immediately attempts to establish a connection to the server (pass a boolean True value as the second argument to establish a connection on demand). See HTTPClient::constructor() and HTTPClient::connect() for information on possible exceptions.

Prototype

JsonRpcClient::constructor(softbool $no_connect = False)

JsonRpcClient::constructor(hash $opts, bool $no_connect = False)

Example
my JsonRpcClient $jrc(("url":"http://hostname/json"));

Table 4.484. Arguments for JsonRpcClient::constructor()

Argument

Description

hash $opts

an option hash, see HTTPClient::constructor() Option Hash Keys for valid keys in this hash.

softbool $no_connect = False

If this optional argument is passed with a value of True, then the object will not attempt to make a connection immediately to the remote socket, but instead will wait until a connection is required and made implicitly or manually established with the parent class' HTTPClient::connect() method.


4.12.2. JsonRpcClient::destructor()

Synopsis

Destroys the JsonRpcClient object and closes any open connections.

Example
delete $jrc;

4.12.3. JsonRpcClient::copy()

Synopsis

Copying objects of this class is not supported, an exception will be thrown.

Table 4.485. Exceptions thrown by JsonRpcClient::copy()

err

desc

JSONRPCCLIENT-COPY-ERROR

objects of this class may not be copied


4.12.4. JsonRpcClient::call()

Synopsis

Calls a remote method using a variable number of arguments for the method arguments and returns the response as qore data structure. See HTTPClient::send(), makeJSONRPCRequestString(), and parseJSON() for information on possible exceptions.

Prototype

JsonRpcClient::call(string $method, ...) returns any

Example
$result = $jrc.call("method_name", $arg1, $arg2);

Table 4.486. Arguments for JsonRpcClient::call()

Argument

Description

string $method

The JSON-RPC method name to call

...

Optional arguments for the method.


Table 4.487. Return Values for JsonRpcClient::call()

Return Type

Description

hash

The information returned by the JSON-RPC method.


4.12.5. JsonRpcClient::callArgs()

Synopsis

Calls a remote method using a single value after the method name for the method arguments and returns the response as qore data structure. See HTTPClient::send(), makeJSONRPCRequestString(), and parseJSON() for information on possible exceptions.

Prototype

JsonRpcClient::callArgs(string $method, any $args) returns any

Example
$result = $jrc.callArgs("method_name", $arg_list);

Table 4.488. Arguments for JsonRpcClient::callArgs()

Argument

Description

string $method

The JSON-RPC method name to call

any $args

An optional list of arguments (or single argument) for the method.


Table 4.489. Return Values for JsonRpcClient::callArgs()

Return Type

Description

any

The information returned by the JSON-RPC method.


4.12.6. JsonRpcClient::callWithInfo()

Synopsis

Like the JsonRpcClient::call() method, except requires an lvalue reference as the first argument that will be used as an output variable providing information about the HTTP request and response made to effect the JSON-RPC call.

Prototype

JsonRpcClient::callWithInfo(reference $info, string $method, ...) returns any

Example
$result = $jrc.callWithInfo(\$info, "method_name", $arg1, $arg2);

Table 4.490. Arguments for JsonRpcClient::callWithInfo()

Argument

Description

reference $info

A reference to an lvalue that will be used as an output variable providing information about the HTTP request and response made to effect the JSON-RPC call.

string $method

The JSON-RPC method name to call

...

Optional arguments for the method.


Table 4.491. Return Values for JsonRpcClient::callWithInfo()

Return Type

Description

hash

The information returned by the JSON-RPC method.


4.12.7. JsonRpcClient::callArgswithInfo()

Synopsis

Like the JsonRpcClient::callArgs() method, except requires an lvalue reference as the first argument that will be used as an output variable providing information about the HTTP request and response made to effect the JSON-RPC call.

Prototype

JsonRpcClient::callArgsWithInfo(reference $info, string $method, any $args) returns any

Example
$result = $jrc.callArgsWithInfo(\$info, "method_name", $arg_list);

Table 4.492. Arguments for JsonRpcClient::callArgsWithInfo()

Argument

Description

reference $info

A reference to an lvalue that will be used as an output variable providing information about the HTTP request and response made to effect the JSON-RPC call.

string $method

The JSON-RPC method name to call

any $args

An optional list of arguments (or single argument) for the method.


Table 4.493. Return Values for JsonRpcClient::callArgsWithInfo()

Return Type

Description

any

The information returned by the JSON-RPC method.


4.12.8. JsonRpcClient::setEventQueue()

Synopsis

Sets a Queue object to receive HTTPClient and Socket events. To remove the event queue and stop monitoring events, pass NOTHING to the method. See Event Handling for more information.

Prototype

JsonRpcClient::setEventQueue() returns nothing

JsonRpcClient::setEventQueue(Queue $queue) returns nothing

Example
$jrc.setEventQueue($queue);

Table 4.494. Arguments for JsonRpcClient::setEventQueue()

Argument

Description

[Queue $queue]

To set the event queue, pass a Queue object; to clear the event queue, pass no argument to the function.