]>
jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blob - lib/Connection.php
10 protected WebSocket\Client
$connection;
12 public function __construct(string $uri, string $api_login, array $options = null)
14 $context = $options["context"] ?? stream_context_create();
16 if (isset($options["tls_verify"]) && !$options["tls_verify"]) {
17 stream_context_set_option($context, 'ssl', 'verify_peer', false);
18 stream_context_set_option($context, 'ssl', 'verify_peer_name', false);
21 $this->connection
= new WebSocket\
Client($uri, [
22 'context' => $context,
24 'Authorization' => sprintf('Basic %s', base64_encode($api_login)),
32 * Encode and send a query to the RPC server.
34 * @note I'm not sure on the response type except that it may be either an object or array.
36 * @param string $method
37 * @param array|null $params
39 * @return object|array|bool
42 public function query(string $method, array|null $params = null): object|array|bool
44 $id = random_int(1, 99999);
53 $json_rpc = json_encode($rpc);
55 $this->connection
->text($json_rpc);
56 $reply = $this->connection
->receive();
58 $reply = json_decode($reply);
60 if (property_exists($reply, 'reply')) {
61 if($id !== $reply->id
) {
62 throw new Exception('Invalid ID. This is not the expected reply.');
64 return $reply->response
;
67 if(property_exists($reply, 'error')) {