This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathClient.php
More file actions
197 lines (168 loc) · 3.81 KB
/
Client.php
File metadata and controls
197 lines (168 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Electrum;
use Electrum\Request\Exception\BadRequestException;
use Electrum\Response\Exception\ElectrumResponseException;
/**
* @author Pascal Krason <pascal.krason@padar.io>
*/
class Client
{
/**
* JSONRPC Host
* @var string
*/
private $host = '';
/**
* JSONRPC Port
* @var int
*/
private $port = 0;
/**
* Last Message-ID
* @var int
*/
private $id = 0;
/**
* @param string $host
* @param int $port
* @param int $id
*/
public function __construct($host = 'http://127.0.0.1', $port = 7777, $id = 0)
{
$this->setHost($host);
$this->setPort($port);
$this->setId($id);
}
/**
* Execute JSONRPC Request
*
* @param $method
* @param array $params
*
* @return mixed
* @throws BadRequestException
* @throws ElectrumResponseException
*/
public function execute($method, $params = [])
{
// Create request payload
$request = $this->createRequest($method, $params);
// Retrieve electrum api response
$response = $this->executeCurlRequest($request);
// Check if an error occured
if(isset($response['error'])) {
// ### Set message
throw ElectrumResponseException::createFromElectrumResponse($response);
}
return $response['result'];
}
/**
* Create request payload
*
* @param $method
* @param array $params
*
* @return mixed
*/
private function createRequest($method, array $params)
{
// Build request string
$request = json_encode([
'method' => $method,
'params' => $params,
'id' => $this->getNextId(),
]);
// Replace braces
return str_replace(['[{', '}]'], ['{', '}'], $request);
}
/**
* Create curl instance & execute the request
* @param $request
*
* @return mixed
* @throws BadRequestException
*/
private function executeCurlRequest($request)
{
// Create CURL instance
$curl = curl_init(vsprintf(
'%s:%s', [$this->getHost(), $this->getPort()]
));
// Set some options we need
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request,
]);
// Execute request & convert data to array
$response = curl_exec($curl);
// Catch error if occured
$error = curl_error($curl);
// Check if request was successfull
if ($error) {
// Set last error, so user can catch it
throw new BadRequestException($error);
}
// Return Data converted to an array
return json_decode($response, true);
}
/**
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* @param string $host
*
* @return Request
*/
public function setHost($host)
{
$this->host = $host;
return $this;
}
/**
* @return int
*/
public function getPort()
{
return $this->port;
}
/**
* @param int $port
*
* @return Request
*/
public function setPort($port)
{
$this->port = $port;
return $this;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return int
*/
public function getNextId()
{
return $this->id++;
}
/**
* @param int $id
*
* @return Request
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
}