-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpRequest.php
384 lines (323 loc) · 7.73 KB
/
HttpRequest.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
namespace Pecee\Http;
class HttpRequest
{
protected $url;
protected $method;
protected $headers;
protected $options;
protected $rawData;
protected $data;
protected $timeout;
protected $returnHeader;
protected $contentType;
public function __construct($url = null)
{
if (function_exists('curl_init') === false) {
throw new \ErrorException('This service requires the CURL PHP extension.');
}
$this->reset();
$this->url = $url;
}
public function reset()
{
$this->url = null;
$this->options = [];
$this->headers = [];
$this->data = [];
$this->rawData = null;
$this->method = null;
$this->returnHeader = true;
$this->contentType = null;
}
/**
* Add header
*
* @param string $header
*
* @return static $this
*/
public function addHeader($header)
{
$this->headers[] = $header;
return $this;
}
/**
* Set headers array
*
* @param array $headers
*
* @return static $this
*/
public function setHeaders(array $headers)
{
$this->headers = $headers;
return $this;
}
/**
* Get all headers
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Add curl option
*
* @param string $option
* @param string $value
*
* @return static $this
*/
public function addOption($option, $value)
{
$this->options[$option] = $value;
return $this;
}
/**
* Set curl options
*
* @param array $options
*
* @return static $this
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}
/**
* Add post data
*
* @param string $key
* @param string $value
*
* @return static $this
*/
public function addPostData($key, $value)
{
$this->data[$key] = $value;
return $this;
}
/**
* Set postdata
*
* @param array $data
*
* @return static $this
*/
public function setPostData(array $data)
{
$this->data = $data;
return $this;
}
/**
* Set raw postdata
*
* @param string $data
*
* @return static $this;
*/
public function setRawPostData($data)
{
$this->rawData = $data;
return $this;
}
/**
* Get post-data
*
* @return array
*/
public function getPostData()
{
return $this->data;
}
/**
* Get raw post-data
*
* @return string
*/
public function getRawPostData()
{
return $this->rawData;
}
/**
* Make post request
*
* @param bool $return
*
* @return HttpResponse $this
*/
public function post($return = false)
{
$this->options[CURLOPT_POST] = true;
return $this->execute($return);
}
/**
* Make get request
*
* @param bool $return
*
* @return HttpResponse
*/
public function get($return = false)
{
return $this->execute($return);
}
/**
* Set timeout
*
* @param int $timeout
*
* @return static $this
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
return $this;
}
/**
* Set method
*
* @param string $method
*
* @return static $this
*/
public function setMethod($method)
{
$this->method = $method;
return $this;
}
/**
* Set content-type
*
* @param string $contentType
*
* @return static $this
*/
public function setContentType($contentType)
{
$this->contentType = strtolower($contentType);
return $this;
}
/**
* Get content-type
*
* @return string
*/
public function getContentType()
{
return $this->contentType;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @param string $url
*
* @return static $this
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Defines if headers should be parsed when receiving response.
*
* @param bool $bool
*
* @return static $this
*/
public function setReturnHeader($bool)
{
$this->returnHeader = $bool;
return $this;
}
/**
* Set basic authentication
*
* @param $username
* @param $password
*
* @return static $this
*/
public function setBasicAuth($username, $password)
{
$this->addHeader('Authorization: Basic ' . base64_encode(sprintf('%s:%s', $username, $password)));
return $this;
}
public function execute($return = true)
{
$handle = curl_init();
if ($this->url === null) {
throw new \InvalidArgumentException('Missing required property: url');
}
if (strtolower($this->method) === 'get' && count($this->getPostData()) > 0) {
$this->url .= ((strpos($this->url, '?') === false) ? '?' : '&');
}
curl_setopt($handle, CURLOPT_URL, $this->url);
$response = new HttpResponse();
if ($this->contentType !== null) {
$this->addHeader('Content-Type: ' . $this->contentType);
}
if ($this->returnHeader === true) {
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_HEADERFUNCTION, [&$response, 'parseHeader']);
}
if ($return === true) {
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
}
if ($this->timeout !== null && $this->timeout > 0) {
// Disable PHP timeout
set_time_limit($this->timeout);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT_MS, $this->timeout);
curl_setopt($handle, CURLOPT_TIMEOUT_MS, $this->timeout);
}
// Add request data
if ($this->method && strtolower($this->method) !== 'get') {
switch ($this->contentType) {
default:
$data = $this->rawData;
break;
case 'application/json':
$data = json_encode($this->data);
break;
case 'application/x-www-form-urlencoded':
$data = http_build_query($this->data);
break;
}
foreach ((array)$this->headers as $key => $header) {
if (stripos($header, 'content-length:') !== false) {
unset($this->headers[$key]);
}
}
$this->addHeader('Content-length: ' . strlen($data));
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
}
// Add headers
if (count($this->headers) > 0) {
curl_setopt($handle, CURLOPT_HTTPHEADER, $this->headers);
}
// Add custom curl options
if (count($this->options) > 0) {
foreach ((array)$this->options as $option => $value) {
curl_setopt($handle, $option, $value);
}
}
$output = curl_exec($handle);
$response->setInfo(curl_getinfo($handle))->setResponse($output, $this->returnHeader);
curl_close($handle);
unset($output, $handle);
return $response;
}
}