1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace JonnyW\PhantomJs\Http; |
11
|
|
|
|
12
|
|
|
use JonnyW\PhantomJs\Exception\InvalidUrlException; |
13
|
|
|
use JonnyW\PhantomJs\Exception\InvalidMethodException; |
14
|
|
|
use JonnyW\PhantomJs\Procedure\InputInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* PHP PhantomJs |
18
|
|
|
* |
19
|
|
|
* @author Jon Wenmoth <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractRequest |
22
|
|
|
implements RequestInterface, InputInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Headers |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
* @access protected |
29
|
|
|
*/ |
30
|
|
|
protected $headers; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Request data |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
* @access protected |
37
|
|
|
*/ |
38
|
|
|
protected $data; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Request URL |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected $url; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Request method |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
* @access protected |
53
|
|
|
*/ |
54
|
|
|
protected $method; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Timeout period |
58
|
|
|
* |
59
|
|
|
* @var int |
60
|
|
|
* @access protected |
61
|
|
|
*/ |
62
|
|
|
protected $timeout; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Page load delay time. |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
* @access protected |
69
|
|
|
*/ |
70
|
|
|
protected $delay; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Viewport width. |
74
|
|
|
* |
75
|
|
|
* @var int |
76
|
|
|
* @access protected |
77
|
|
|
*/ |
78
|
|
|
protected $viewportWidth; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Viewport height. |
82
|
|
|
* |
83
|
|
|
* @var int |
84
|
|
|
* @access protected |
85
|
|
|
*/ |
86
|
|
|
protected $viewportHeight; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Body styles. |
90
|
|
|
* |
91
|
|
|
* @var int |
92
|
|
|
* @access protected |
93
|
|
|
*/ |
94
|
|
|
protected $bodyStyles; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Internal constructor |
98
|
|
|
* |
99
|
|
|
* @access public |
100
|
|
|
* @param string $url (default: null) |
101
|
|
|
* @param string $method (default: RequestInterface::METHOD_GET) |
102
|
|
|
* @param int $timeout (default: 5000) |
103
|
|
|
*/ |
104
|
113 |
|
public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000) |
105
|
|
|
{ |
106
|
113 |
|
$this->headers = array(); |
107
|
113 |
|
$this->data = array(); |
108
|
113 |
|
$this->bodyStyles = array(); |
|
|
|
|
109
|
113 |
|
$this->delay = 0; |
110
|
113 |
|
$this->viewportWidth = 0; |
111
|
113 |
|
$this->viewportHeight = 0; |
112
|
|
|
|
113
|
113 |
|
$this->setMethod($method); |
114
|
113 |
|
$this->setTimeout($timeout); |
115
|
|
|
|
116
|
113 |
|
if ($url) { |
|
|
|
|
117
|
8 |
|
$this->setUrl($url); |
118
|
8 |
|
} |
119
|
113 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set request method |
123
|
|
|
* |
124
|
|
|
* @access public |
125
|
|
|
* @param string $method |
126
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
127
|
|
|
* @throws \JonnyW\PhantomJs\Exception\InvalidMethodException |
128
|
|
|
*/ |
129
|
113 |
|
public function setMethod($method) |
130
|
|
|
{ |
131
|
113 |
|
$method = strtoupper($method); |
132
|
113 |
|
$reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface'); |
133
|
|
|
|
134
|
113 |
|
if (!$reflection->hasConstant('METHOD_' . $method)) { |
135
|
3 |
|
throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method)); |
136
|
|
|
} |
137
|
|
|
|
138
|
113 |
|
$this->method = $method; |
139
|
|
|
|
140
|
113 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get request method |
145
|
|
|
* |
146
|
|
|
* @access public |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
56 |
|
public function getMethod() |
150
|
|
|
{ |
151
|
56 |
|
return $this->method; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set timeout period |
156
|
|
|
* |
157
|
|
|
* @access public |
158
|
|
|
* @param int $timeout |
159
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
160
|
|
|
*/ |
161
|
113 |
|
public function setTimeout($timeout) |
162
|
|
|
{ |
163
|
113 |
|
$this->timeout = $timeout; |
164
|
|
|
|
165
|
113 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get timeout period |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* @return int |
173
|
|
|
*/ |
174
|
31 |
|
public function getTimeout() |
175
|
|
|
{ |
176
|
31 |
|
return $this->timeout; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set page load delay time (seconds). |
181
|
|
|
* |
182
|
|
|
* @access public |
183
|
|
|
* @param int $delay |
184
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
185
|
|
|
*/ |
186
|
6 |
|
public function setDelay($delay) |
187
|
|
|
{ |
188
|
6 |
|
$this->delay = (int) $delay; |
189
|
|
|
|
190
|
6 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get page load delay time (seconds). |
195
|
|
|
* |
196
|
|
|
* @access public |
197
|
|
|
* @return int |
198
|
|
|
*/ |
199
|
25 |
|
public function getDelay() |
200
|
|
|
{ |
201
|
25 |
|
return (int) $this->delay; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set viewport size. |
206
|
|
|
* |
207
|
|
|
* @access public |
208
|
|
|
* @param int $width |
209
|
|
|
* @param int $height |
210
|
|
|
* @return void |
211
|
|
|
*/ |
212
|
8 |
|
public function setViewportSize($width, $height) |
213
|
|
|
{ |
214
|
8 |
|
$this->viewportWidth = (int) $width; |
215
|
8 |
|
$this->viewportHeight = (int) $height; |
216
|
|
|
|
217
|
8 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Get viewport width. |
222
|
|
|
* |
223
|
|
|
* @access public |
224
|
|
|
* @return int |
225
|
|
|
*/ |
226
|
28 |
|
public function getViewportWidth() |
227
|
|
|
{ |
228
|
28 |
|
return (int) $this->viewportWidth; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get viewport height. |
233
|
|
|
* |
234
|
|
|
* @access public |
235
|
|
|
* @return int |
236
|
|
|
*/ |
237
|
28 |
|
public function getViewportHeight() |
238
|
|
|
{ |
239
|
28 |
|
return (int) $this->viewportHeight; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Set request URL |
244
|
|
|
* |
245
|
|
|
* @access public |
246
|
|
|
* @param string $url |
247
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
248
|
|
|
* @throws \JonnyW\PhantomJs\Exception\InvalidUrlException |
249
|
|
|
*/ |
250
|
52 |
|
public function setUrl($url) |
251
|
|
|
{ |
252
|
52 |
|
if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { |
253
|
3 |
|
throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url)); |
254
|
|
|
} |
255
|
|
|
|
256
|
49 |
|
$this->url = $url; |
257
|
|
|
|
258
|
49 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get request URL |
263
|
|
|
* - Assembles query string for GET |
264
|
|
|
* and HEAD requests |
265
|
|
|
* |
266
|
|
|
* @access public |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
42 |
|
public function getUrl() |
270
|
|
|
{ |
271
|
42 |
|
if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) { |
272
|
4 |
|
return $this->url; |
273
|
|
|
} |
274
|
|
|
|
275
|
38 |
|
$url = $this->url; |
276
|
|
|
|
277
|
38 |
|
if (count($this->data)) { |
278
|
|
|
|
279
|
10 |
|
$url .= false === strpos($url, '?') ? '?' : '&'; |
280
|
10 |
|
$url .= http_build_query($this->data); |
281
|
10 |
|
} |
282
|
|
|
|
283
|
38 |
|
return $url; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get content body |
288
|
|
|
* - Returns query string if not GET or HEAD |
289
|
|
|
* |
290
|
|
|
* @access public |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
34 |
|
public function getBody() |
294
|
|
|
{ |
295
|
34 |
|
if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) { |
296
|
30 |
|
return ''; |
297
|
|
|
} |
298
|
|
|
|
299
|
4 |
|
return http_build_query($this->getRequestData()); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set request data |
304
|
|
|
* |
305
|
|
|
* @access public |
306
|
|
|
* @param array $data |
307
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
308
|
|
|
*/ |
309
|
29 |
|
public function setRequestData(array $data) |
310
|
|
|
{ |
311
|
29 |
|
$this->data = $data; |
312
|
|
|
|
313
|
29 |
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Get request data |
318
|
|
|
* |
319
|
|
|
* @access public |
320
|
|
|
* @param boolean $flat |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
10 |
|
public function getRequestData($flat = true) |
324
|
|
|
{ |
325
|
10 |
|
if ($flat) { |
326
|
7 |
|
return $this->flattenData($this->data); |
327
|
|
|
} |
328
|
|
|
|
329
|
3 |
|
return $this->data; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Set headers |
334
|
|
|
* |
335
|
|
|
* @access public |
336
|
|
|
* @param array $headers |
337
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
338
|
|
|
*/ |
339
|
9 |
|
public function setHeaders(array $headers) |
340
|
|
|
{ |
341
|
9 |
|
$this->headers = $headers; |
342
|
9 |
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Add single header |
346
|
|
|
* |
347
|
|
|
* @access public |
348
|
|
|
* @param string $header |
349
|
|
|
* @param string $value |
350
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
351
|
|
|
*/ |
352
|
1 |
|
public function addHeader($header, $value) |
353
|
|
|
{ |
354
|
1 |
|
$this->headers[$header] = $value; |
355
|
|
|
|
356
|
1 |
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Merge headers with existing |
361
|
|
|
* |
362
|
|
|
* @access public |
363
|
|
|
* @param array $headers |
364
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
365
|
|
|
*/ |
366
|
3 |
|
public function addHeaders(array $headers) |
367
|
|
|
{ |
368
|
3 |
|
$this->headers = array_merge($this->headers, $headers); |
369
|
|
|
|
370
|
3 |
|
return $this; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Get request headers |
375
|
|
|
* |
376
|
|
|
* @access public |
377
|
|
|
* @param string $format |
378
|
|
|
* @return array |
379
|
|
|
*/ |
380
|
35 |
|
public function getHeaders($format = 'default') |
381
|
|
|
{ |
382
|
35 |
|
if ($format === 'json') { |
383
|
29 |
|
return json_encode($this->headers); |
|
|
|
|
384
|
|
|
} |
385
|
|
|
|
386
|
6 |
|
return $this->headers; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Set body styles |
391
|
|
|
* |
392
|
|
|
* @access public |
393
|
|
|
* @param array $styles |
394
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
395
|
|
|
*/ |
396
|
1 |
|
public function setBodyStyles(array $styles) |
397
|
|
|
{ |
398
|
1 |
|
$this->bodyStyles = $styles; |
|
|
|
|
399
|
|
|
|
400
|
1 |
|
return $this; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Get body styles |
405
|
|
|
* |
406
|
|
|
* @access public |
407
|
|
|
* @param string $format (default: 'default') |
408
|
|
|
* @return array |
409
|
|
|
*/ |
410
|
25 |
|
public function getBodyStyles($format = 'default') |
411
|
|
|
{ |
412
|
25 |
|
if ($format === 'json') { |
413
|
25 |
|
return json_encode($this->bodyStyles); |
|
|
|
|
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
return $this->bodyStyles; |
|
|
|
|
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Flatten data into single |
421
|
|
|
* dimensional array |
422
|
|
|
* |
423
|
|
|
* @access protected |
424
|
|
|
* @param array $data |
425
|
|
|
* @param string $prefix |
426
|
|
|
* @param string $format |
427
|
|
|
* @return array |
428
|
|
|
*/ |
429
|
7 |
|
protected function flattenData(array $data, $prefix = '', $format = '%s') |
430
|
|
|
{ |
431
|
7 |
|
$flat = array(); |
432
|
|
|
|
433
|
7 |
|
foreach ($data as $name => $value) { |
434
|
|
|
|
435
|
7 |
|
$ref = $prefix . sprintf($format, $name); |
436
|
|
|
|
437
|
7 |
|
if (is_array($value)) { |
438
|
|
|
|
439
|
3 |
|
$flat += $this->flattenData($value, $ref, '[%s]'); |
440
|
3 |
|
continue; |
441
|
|
|
} |
442
|
|
|
|
443
|
7 |
|
$flat[$ref] = $value; |
444
|
7 |
|
} |
445
|
|
|
|
446
|
7 |
|
return $flat; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..