]>
jfr.im git - irc/unrealircd/unrealircd-rpc-php.git/blob - lib/Connection.php
d9a7342a531d8103d7c6adcbad84f6ce72ddb0b9
10 protected WebSocket\Client
$connection;
12 public function __construct(string $uri, string $api_login, array $options)
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 $params
38 * @return object|array|bool
41 public function query(string $method, array $params): object|array|bool
43 $id = random_int(1, 99999);
52 $json_rpc = json_encode($rpc);
54 $this->connection
->text($json_rpc);
55 $reply = $this->connection
->receive();
57 $reply = json_decode($reply);
59 if (property_exists($reply, 'reply')) {
60 if($id !== $reply->id
) {
61 throw new Exception('Invalid ID. This is not the expected reply.');
63 return $reply->response
;
66 if(property_exists($reply, 'error')) {