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\InvalidMethodException; |
13
|
|
|
use JonnyW\PhantomJs\Procedure\InputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PHP PhantomJs |
17
|
|
|
* |
18
|
|
|
* @author Jon Wenmoth <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractRequest |
21
|
|
|
implements RequestInterface, InputInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Headers |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
* @access protected |
28
|
|
|
*/ |
29
|
|
|
protected $headers; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Settings |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
* @access protected |
36
|
|
|
*/ |
37
|
|
|
protected $settings; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Cookies |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
* @access protected |
44
|
|
|
*/ |
45
|
|
|
protected $cookies; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Request data |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
* @access protected |
52
|
|
|
*/ |
53
|
|
|
protected $data; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Request URL |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
* @access protected |
60
|
|
|
*/ |
61
|
|
|
protected $url; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Request method |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
* @access protected |
68
|
|
|
*/ |
69
|
|
|
protected $method; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Timeout period |
73
|
|
|
* |
74
|
|
|
* @var int |
75
|
|
|
* @access protected |
76
|
|
|
*/ |
77
|
|
|
protected $timeout; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Page load delay time. |
81
|
|
|
* |
82
|
|
|
* @var int |
83
|
|
|
* @access protected |
84
|
|
|
*/ |
85
|
|
|
protected $delay; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Viewport width. |
89
|
|
|
* |
90
|
|
|
* @var int |
91
|
|
|
* @access protected |
92
|
|
|
*/ |
93
|
|
|
protected $viewportWidth; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Viewport height. |
97
|
|
|
* |
98
|
|
|
* @var int |
99
|
|
|
* @access protected |
100
|
|
|
*/ |
101
|
|
|
protected $viewportHeight; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Body styles. |
105
|
|
|
* |
106
|
|
|
* @var array |
107
|
|
|
* @access protected |
108
|
|
|
*/ |
109
|
|
|
protected $bodyStyles; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Internal constructor |
113
|
|
|
* |
114
|
|
|
* @access public |
115
|
|
|
* @param string $url (default: null) |
116
|
|
|
* @param string $method (default: RequestInterface::METHOD_GET) |
117
|
|
|
* @param int $timeout (default: 5000) |
118
|
|
|
*/ |
119
|
125 |
|
public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000) |
120
|
|
|
{ |
121
|
125 |
|
$this->headers = array(); |
122
|
125 |
|
$this->data = array(); |
123
|
125 |
|
$this->bodyStyles = array(); |
124
|
125 |
|
$this->settings = array(); |
125
|
125 |
|
$this->delay = 0; |
126
|
125 |
|
$this->viewportWidth = 0; |
127
|
125 |
|
$this->viewportHeight = 0; |
128
|
|
|
|
129
|
125 |
|
$this->cookies = array( |
130
|
125 |
|
'add' => array(), |
131
|
125 |
|
'delete' => array() |
132
|
125 |
|
); |
133
|
|
|
|
134
|
125 |
|
$this->setMethod($method); |
135
|
125 |
|
$this->setTimeout($timeout); |
136
|
|
|
|
137
|
125 |
|
if ($url) { |
|
|
|
|
138
|
8 |
|
$this->setUrl($url); |
139
|
8 |
|
} |
140
|
125 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set request method |
144
|
|
|
* |
145
|
|
|
* @access public |
146
|
|
|
* @param string $method |
147
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
148
|
|
|
* @throws \JonnyW\PhantomJs\Exception\InvalidMethodException |
149
|
|
|
*/ |
150
|
125 |
|
public function setMethod($method) |
151
|
|
|
{ |
152
|
125 |
|
$method = strtoupper($method); |
153
|
125 |
|
$reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface'); |
154
|
|
|
|
155
|
125 |
|
if (!$reflection->hasConstant('METHOD_' . $method)) { |
156
|
3 |
|
throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method)); |
157
|
|
|
} |
158
|
|
|
|
159
|
125 |
|
$this->method = $method; |
160
|
|
|
|
161
|
125 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get request method |
166
|
|
|
* |
167
|
|
|
* @access public |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
66 |
|
public function getMethod() |
171
|
|
|
{ |
172
|
66 |
|
return $this->method; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set timeout period |
177
|
|
|
* |
178
|
|
|
* @access public |
179
|
|
|
* @param int $timeout |
180
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
181
|
|
|
*/ |
182
|
125 |
|
public function setTimeout($timeout) |
183
|
|
|
{ |
184
|
125 |
|
$this->settings['resourceTimeout'] = $timeout; |
185
|
|
|
|
186
|
125 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get timeout period |
191
|
|
|
* |
192
|
|
|
* @access public |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
8 |
|
public function getTimeout() |
196
|
|
|
{ |
197
|
8 |
|
if (isset($this->settings['resourceTimeout'])) { |
198
|
8 |
|
return $this->settings['resourceTimeout']; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return null; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set page load delay time (seconds). |
206
|
|
|
* |
207
|
|
|
* @access public |
208
|
|
|
* @param int $delay |
209
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
210
|
|
|
*/ |
211
|
6 |
|
public function setDelay($delay) |
212
|
|
|
{ |
213
|
6 |
|
$this->delay = (int) $delay; |
214
|
|
|
|
215
|
6 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get page load delay time (seconds). |
220
|
|
|
* |
221
|
|
|
* @access public |
222
|
|
|
* @return int |
223
|
|
|
*/ |
224
|
33 |
|
public function getDelay() |
225
|
|
|
{ |
226
|
33 |
|
return (int) $this->delay; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Set viewport size. |
231
|
|
|
* |
232
|
|
|
* @access public |
233
|
|
|
* @param int $width |
234
|
|
|
* @param int $height |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
8 |
|
public function setViewportSize($width, $height) |
238
|
|
|
{ |
239
|
8 |
|
$this->viewportWidth = (int) $width; |
240
|
8 |
|
$this->viewportHeight = (int) $height; |
241
|
|
|
|
242
|
8 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get viewport width. |
247
|
|
|
* |
248
|
|
|
* @access public |
249
|
|
|
* @return int |
250
|
|
|
*/ |
251
|
38 |
|
public function getViewportWidth() |
252
|
|
|
{ |
253
|
38 |
|
return (int) $this->viewportWidth; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get viewport height. |
258
|
|
|
* |
259
|
|
|
* @access public |
260
|
|
|
* @return int |
261
|
|
|
*/ |
262
|
38 |
|
public function getViewportHeight() |
263
|
|
|
{ |
264
|
38 |
|
return (int) $this->viewportHeight; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Set request URL |
269
|
|
|
* |
270
|
|
|
* @access public |
271
|
|
|
* @param string $url |
272
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
273
|
|
|
*/ |
274
|
59 |
|
public function setUrl($url) |
275
|
|
|
{ |
276
|
59 |
|
$this->url = $url; |
277
|
|
|
|
278
|
59 |
|
return $this; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Get request URL |
283
|
|
|
* - Assembles query string for GET |
284
|
|
|
* and HEAD requests |
285
|
|
|
* |
286
|
|
|
* @access public |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
52 |
|
public function getUrl() |
290
|
|
|
{ |
291
|
52 |
|
if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) { |
292
|
4 |
|
return $this->url; |
293
|
|
|
} |
294
|
|
|
|
295
|
48 |
|
$url = $this->url; |
296
|
|
|
|
297
|
48 |
|
if (count($this->data)) { |
298
|
|
|
|
299
|
10 |
|
$url .= false === strpos($url, '?') ? '?' : '&'; |
300
|
10 |
|
$url .= http_build_query($this->data); |
301
|
10 |
|
} |
302
|
|
|
|
303
|
48 |
|
return $url; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get content body |
308
|
|
|
* - Returns query string if not GET or HEAD |
309
|
|
|
* |
310
|
|
|
* @access public |
311
|
|
|
* @return string |
312
|
|
|
*/ |
313
|
44 |
|
public function getBody() |
314
|
|
|
{ |
315
|
44 |
|
if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) { |
316
|
40 |
|
return ''; |
317
|
|
|
} |
318
|
|
|
|
319
|
4 |
|
return http_build_query($this->getRequestData()); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set request data |
324
|
|
|
* |
325
|
|
|
* @access public |
326
|
|
|
* @param array $data |
327
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
328
|
|
|
*/ |
329
|
29 |
|
public function setRequestData(array $data) |
330
|
|
|
{ |
331
|
29 |
|
$this->data = $data; |
332
|
|
|
|
333
|
29 |
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Get request data |
338
|
|
|
* |
339
|
|
|
* @access public |
340
|
|
|
* @param boolean $flat |
341
|
|
|
* @return array |
342
|
|
|
*/ |
343
|
10 |
|
public function getRequestData($flat = true) |
344
|
|
|
{ |
345
|
10 |
|
if ($flat) { |
346
|
7 |
|
return $this->flattenData($this->data); |
347
|
|
|
} |
348
|
|
|
|
349
|
3 |
|
return $this->data; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Set headers |
354
|
|
|
* |
355
|
|
|
* @access public |
356
|
|
|
* @param array $headers |
357
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
358
|
|
|
*/ |
359
|
9 |
|
public function setHeaders(array $headers) |
360
|
|
|
{ |
361
|
9 |
|
$this->headers = $headers; |
362
|
9 |
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Add single header |
366
|
|
|
* |
367
|
|
|
* @access public |
368
|
|
|
* @param string $header |
369
|
|
|
* @param string $value |
370
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
371
|
|
|
*/ |
372
|
1 |
|
public function addHeader($header, $value) |
373
|
|
|
{ |
374
|
1 |
|
$this->headers[$header] = $value; |
375
|
|
|
|
376
|
1 |
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Merge headers with existing |
381
|
|
|
* |
382
|
|
|
* @access public |
383
|
|
|
* @param array $headers |
384
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
385
|
|
|
*/ |
386
|
3 |
|
public function addHeaders(array $headers) |
387
|
|
|
{ |
388
|
3 |
|
$this->headers = array_merge($this->headers, $headers); |
389
|
|
|
|
390
|
3 |
|
return $this; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Get request headers |
395
|
|
|
* |
396
|
|
|
* @access public |
397
|
|
|
* @param string $format |
398
|
|
|
* @return array|string |
399
|
|
|
*/ |
400
|
45 |
|
public function getHeaders($format = 'default') |
401
|
|
|
{ |
402
|
45 |
|
if ($format === 'json') { |
403
|
4 |
|
return json_encode($this->headers); |
404
|
|
|
} |
405
|
|
|
|
406
|
41 |
|
return $this->headers; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Add single setting |
411
|
|
|
* |
412
|
|
|
* @access public |
413
|
|
|
* @param string $setting |
414
|
|
|
* @param string $value |
415
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
416
|
|
|
*/ |
417
|
2 |
|
public function addSetting($setting, $value) |
418
|
|
|
{ |
419
|
2 |
|
$this->settings[$setting] = $value; |
420
|
|
|
|
421
|
2 |
|
return $this; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Get settings |
426
|
|
|
* |
427
|
|
|
* @access public |
428
|
|
|
* @return array |
429
|
|
|
*/ |
430
|
37 |
|
public function getSettings() |
431
|
|
|
{ |
432
|
37 |
|
return $this->settings; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Add cookie. |
437
|
|
|
* |
438
|
|
|
* @access public |
439
|
|
|
* @param string $name |
440
|
|
|
* @param mixed $value |
441
|
|
|
* @param string $path |
442
|
|
|
* @param string $domain |
443
|
|
|
* @param bool $httpOnly (default: true) |
444
|
|
|
* @param bool $secure (default: false) |
445
|
|
|
* @param int $expires (default: null) |
446
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
447
|
|
|
*/ |
448
|
|
|
public function addCookie($name, $value, $path, $domain, $httpOnly = true, $secure = false, $expires = null) |
449
|
|
|
{ |
450
|
7 |
|
$filter = function ($value) { |
451
|
7 |
|
return !is_null($value); |
452
|
7 |
|
}; |
453
|
|
|
|
454
|
7 |
|
$this->cookies['add'][] = array_filter(array( |
455
|
7 |
|
'name' => $name, |
456
|
7 |
|
'value' => $value, |
457
|
7 |
|
'path' => $path, |
458
|
7 |
|
'domain' => $domain, |
459
|
7 |
|
'httponly' => $httpOnly, |
460
|
7 |
|
'secure' => $secure, |
461
|
|
|
'expires' => $expires |
462
|
7 |
|
), $filter); |
463
|
|
|
|
464
|
7 |
|
return $this; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Delete cookie. |
469
|
|
|
* |
470
|
|
|
* @access public |
471
|
|
|
* @param string $name |
472
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
473
|
|
|
*/ |
474
|
3 |
|
public function deleteCookie($name) |
475
|
|
|
{ |
476
|
3 |
|
$this->cookies['delete'][] = $name; |
477
|
|
|
|
478
|
3 |
|
return $this; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Get cookies |
483
|
|
|
* |
484
|
|
|
* @access public |
485
|
|
|
* @return array |
486
|
|
|
*/ |
487
|
37 |
|
public function getCookies() |
488
|
|
|
{ |
489
|
37 |
|
return $this->cookies; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* Set body styles |
494
|
|
|
* |
495
|
|
|
* @access public |
496
|
|
|
* @param array $styles |
497
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
498
|
|
|
*/ |
499
|
1 |
|
public function setBodyStyles(array $styles) |
500
|
|
|
{ |
501
|
1 |
|
$this->bodyStyles = $styles; |
502
|
|
|
|
503
|
1 |
|
return $this; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Get body styles |
508
|
|
|
* |
509
|
|
|
* @access public |
510
|
|
|
* @param string $format (default: 'default') |
511
|
|
|
* @return array|string |
512
|
|
|
*/ |
513
|
35 |
|
public function getBodyStyles($format = 'default') |
514
|
|
|
{ |
515
|
35 |
|
if ($format === 'json') { |
516
|
|
|
return json_encode($this->bodyStyles); |
517
|
|
|
} |
518
|
|
|
|
519
|
35 |
|
return $this->bodyStyles; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Flatten data into single |
524
|
|
|
* dimensional array |
525
|
|
|
* |
526
|
|
|
* @access protected |
527
|
|
|
* @param array $data |
528
|
|
|
* @param string $prefix |
529
|
|
|
* @param string $format |
530
|
|
|
* @return array |
531
|
|
|
*/ |
532
|
7 |
|
protected function flattenData(array $data, $prefix = '', $format = '%s') |
533
|
|
|
{ |
534
|
7 |
|
$flat = array(); |
535
|
|
|
|
536
|
7 |
|
foreach ($data as $name => $value) { |
537
|
|
|
|
538
|
7 |
|
$ref = $prefix . sprintf($format, $name); |
539
|
|
|
|
540
|
7 |
|
if (is_array($value)) { |
541
|
|
|
|
542
|
3 |
|
$flat += $this->flattenData($value, $ref, '[%s]'); |
543
|
3 |
|
continue; |
544
|
|
|
} |
545
|
|
|
|
546
|
7 |
|
$flat[$ref] = $value; |
547
|
7 |
|
} |
548
|
|
|
|
549
|
7 |
|
return $flat; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: