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