]>
Commit | Line | Data |
---|---|---|
562c96c0 BM |
1 | <?php |
2 | ||
3 | namespace UnrealIRCdRPC; | |
4 | ||
5 | use WebSocket; | |
6 | ||
7 | class Connection | |
8 | { | |
9 | protected $conn; | |
10 | ||
11 | public function __construct(string $uri, string $api_login, array $options = []) | |
12 | { | |
13 | if (isset($options["context"])) | |
14 | $context = $options["context"]; | |
15 | else | |
16 | $context = stream_context_create(); | |
17 | ||
18 | if (isset($options["tls_verify"]) && ($options["tls_verify"] == FALSE)) | |
19 | { | |
20 | stream_context_set_option($context, 'ssl', 'verify_peer', false); | |
21 | stream_context_set_option($context, 'ssl', 'verify_peer_name', false); | |
22 | } | |
23 | ||
24 | $this->conn = new WebSocket\Client($uri, [ | |
25 | 'context' => $context, | |
26 | 'headers' => [ | |
27 | 'Authorization' => 'Basic '.base64_encode($api_login), | |
28 | ], | |
29 | 'timeout' => 10, | |
30 | ]); | |
31 | ||
32 | } | |
33 | ||
34 | public function query(string $method, array $params = []) | |
35 | { | |
36 | $rpc = Array("jsonrpc" => "2.0", | |
37 | "method" => $method, | |
38 | "params" => $params, | |
39 | "id" => 123); | |
40 | $json_rpc = json_encode($rpc); | |
41 | $this->conn->text($json_rpc); | |
42 | $reply = $this->conn->receive(); | |
43 | $reply = json_decode($reply); | |
44 | if ($reply->response) | |
45 | return $reply->response; | |
46 | return FALSE; // throw error? | |
47 | } | |
48 | } |