1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @category Brownie/HttpClient |
4
|
|
|
* @author Brownie <[email protected]> |
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Brownie\HttpClient; |
9
|
|
|
|
10
|
|
|
use Brownie\HttpClient\Exception\ValidateException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* HTTP client request. |
14
|
|
|
*/ |
15
|
|
|
class Request |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Body data formats. |
20
|
|
|
*/ |
21
|
|
|
const FORMAT_APPLICATION_JSON = 'application/json'; |
22
|
|
|
|
23
|
|
|
const FORMAT_APPLICATION_XML = 'application/xml'; |
24
|
|
|
|
25
|
|
|
const FORMAT_TEXT_HTML = 'text/html'; |
26
|
|
|
|
27
|
|
|
const FORMAT_TEXT_PLAIN = 'text/plain'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* HTTP request method GET. |
31
|
|
|
*/ |
32
|
|
|
const HTTP_METHOD_GET = 'GET'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* HTTP request method POST. |
36
|
|
|
*/ |
37
|
|
|
const HTTP_METHOD_POST = 'POST'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* HTTP request method PUT. |
41
|
|
|
*/ |
42
|
|
|
const HTTP_METHOD_PUT = 'PUT'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* HTTP request method DELETE. |
46
|
|
|
*/ |
47
|
|
|
const HTTP_METHOD_DELETE = 'DELETE'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* HTTP request method. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $method = self::HTTP_METHOD_GET; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* URL request. |
58
|
|
|
* |
59
|
|
|
* @var null|string |
60
|
|
|
*/ |
61
|
|
|
private $url = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Request body. |
65
|
|
|
* |
66
|
|
|
* @var null|string |
67
|
|
|
*/ |
68
|
|
|
private $body = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Body data format. |
72
|
|
|
* |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
private $bodyFormat = self::FORMAT_APPLICATION_JSON; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The maximum number of seconds allowed to execute a query. |
79
|
|
|
* |
80
|
|
|
* @var int |
81
|
|
|
*/ |
82
|
|
|
private $timeOut = 60; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* An array of HTTP header fields to set. |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
private $headers = array(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* An array of GET params to set. |
93
|
|
|
* |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
private $params = array(); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Flag of checking the server's SSL. |
100
|
|
|
* |
101
|
|
|
* @var bool |
102
|
|
|
*/ |
103
|
|
|
private $disableSSLValidation = false; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Authentication data. |
107
|
|
|
* |
108
|
|
|
* @var string |
109
|
|
|
*/ |
110
|
|
|
private $authentication; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Sets HTTP request method. |
114
|
|
|
* Returns the current object. |
115
|
|
|
* |
116
|
|
|
* @param $method |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
1 |
|
public function setMethod($method) |
121
|
|
|
{ |
122
|
1 |
|
$this->method = $method; |
123
|
1 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sets URL request. |
128
|
|
|
* Returns the current object. |
129
|
|
|
* |
130
|
|
|
* @param string $url URL request. |
131
|
|
|
* |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
2 |
|
public function setUrl($url) |
135
|
|
|
{ |
136
|
2 |
|
$this->url = $url; |
137
|
2 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets request body. |
142
|
|
|
* Returns the current object. |
143
|
|
|
* |
144
|
|
|
* @param string $body Request body. |
145
|
|
|
* |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
1 |
|
public function setBody($body) |
149
|
|
|
{ |
150
|
1 |
|
$this->body = $body; |
151
|
1 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Adds GET param. |
156
|
|
|
* Returns the current object. |
157
|
|
|
* |
158
|
|
|
* @param string $name GET parameter name. |
159
|
|
|
* @param string $value GET parameter value. |
160
|
|
|
* |
161
|
|
|
* @return self |
162
|
|
|
*/ |
163
|
1 |
|
public function addParam($name, $value) |
164
|
|
|
{ |
165
|
1 |
|
$this->params[$name] = $value; |
166
|
1 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sets the body data format. |
171
|
|
|
* Returns the current object. |
172
|
|
|
* |
173
|
|
|
* @param string $bodyFormat Body data format. |
174
|
|
|
* |
175
|
|
|
* @return self |
176
|
|
|
*/ |
177
|
1 |
|
public function setBodyFormat($bodyFormat) |
178
|
|
|
{ |
179
|
1 |
|
$this->bodyFormat = $bodyFormat; |
180
|
1 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Sets the execution time of the request. |
185
|
|
|
* Returns the current object. |
186
|
|
|
* |
187
|
|
|
* @param int $timeOut Time in seconds. |
188
|
|
|
* |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
1 |
|
public function setTimeOut($timeOut) |
192
|
|
|
{ |
193
|
1 |
|
$this->timeOut = $timeOut; |
194
|
1 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Adds header. |
199
|
|
|
* Returns the current object. |
200
|
|
|
* |
201
|
|
|
* @param string $name Header parameter name. |
202
|
|
|
* @param string $value Header parameter value. |
203
|
|
|
* |
204
|
|
|
* @return self |
205
|
|
|
*/ |
206
|
1 |
|
public function addHeader($name, $value) |
207
|
|
|
{ |
208
|
1 |
|
$this->headers[$name] = $value; |
209
|
1 |
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns HTTP request method. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
1 |
|
public function getMethod() |
218
|
|
|
{ |
219
|
1 |
|
return $this->method; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns URL request. |
224
|
|
|
* |
225
|
|
|
* @return null|string |
226
|
|
|
*/ |
227
|
3 |
|
public function getUrl() |
228
|
|
|
{ |
229
|
3 |
|
return $this->url; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns request body. |
234
|
|
|
* |
235
|
|
|
* @return null|string |
236
|
|
|
*/ |
237
|
1 |
|
public function getBody() |
238
|
|
|
{ |
239
|
1 |
|
return $this->body; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Returns GET params. |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
1 |
|
public function getParams() |
248
|
|
|
{ |
249
|
1 |
|
return $this->params; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Returns body data format. |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
1 |
|
public function getBodyFormat() |
258
|
|
|
{ |
259
|
1 |
|
return $this->bodyFormat; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Returns the maximum time to execute the query. |
264
|
|
|
* |
265
|
|
|
* @return int |
266
|
|
|
*/ |
267
|
1 |
|
public function getTimeOut() |
268
|
|
|
{ |
269
|
1 |
|
return $this->timeOut; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Returns headers. |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
1 |
|
public function getHeaders() |
278
|
|
|
{ |
279
|
1 |
|
return $this->headers; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Disables of checking the server's SSL. |
284
|
|
|
* Returns the current object. |
285
|
|
|
* |
286
|
|
|
* @return self |
287
|
|
|
*/ |
288
|
1 |
|
public function disableSSLValidation() |
289
|
|
|
{ |
290
|
1 |
|
$this->disableSSLValidation = true; |
291
|
1 |
|
return $this; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Returns flag of checking the server's SSL. |
296
|
|
|
* |
297
|
|
|
* @return bool |
298
|
|
|
*/ |
299
|
1 |
|
public function isDisableSSLValidation() |
300
|
|
|
{ |
301
|
1 |
|
return $this->disableSSLValidation; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Validates request data. |
306
|
|
|
* |
307
|
|
|
* @throws ValidateException |
308
|
|
|
*/ |
309
|
2 |
|
public function validate() |
310
|
|
|
{ |
311
|
2 |
|
$url = $this->getUrl(); |
312
|
2 |
|
if (empty($url)) { |
313
|
1 |
|
throw new ValidateException('No required fields: url'); |
314
|
|
|
} |
315
|
1 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Sets authentication data. |
319
|
|
|
* Returns the current object. |
320
|
|
|
* |
321
|
|
|
* @param $login |
322
|
|
|
* @param $password |
323
|
|
|
* |
324
|
|
|
* @return self |
325
|
|
|
*/ |
326
|
1 |
|
public function setAuthentication($login, $password) |
327
|
|
|
{ |
328
|
1 |
|
$this->authentication = $login . ':' . $password; |
329
|
1 |
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Returns authentication data. |
334
|
|
|
* |
335
|
|
|
* @return string |
336
|
|
|
*/ |
337
|
1 |
|
public function getAuthentication() |
338
|
|
|
{ |
339
|
1 |
|
return $this->authentication; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|