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
|
|
|
namespace JonnyW\PhantomJs\Http; |
10
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Procedure\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PHP PhantomJs |
15
|
|
|
* |
16
|
|
|
* @author Jon Wenmoth <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Response |
19
|
|
|
implements ResponseInterface, OutputInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Http headers array |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
* @access public |
26
|
|
|
*/ |
27
|
|
|
public $headers; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Response int |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
* @access public |
34
|
|
|
*/ |
35
|
|
|
public $status; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Response body |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
* @access public |
42
|
|
|
*/ |
43
|
|
|
public $content; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Response content type header |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
* @access public |
50
|
|
|
*/ |
51
|
|
|
public $contentType; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Requested URL |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
* @access public |
58
|
|
|
*/ |
59
|
|
|
public $url; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Redirected URL |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
* @access public |
66
|
|
|
*/ |
67
|
|
|
public $redirectURL; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Request time string |
71
|
|
|
* |
72
|
|
|
* @var string |
73
|
|
|
* @access public |
74
|
|
|
*/ |
75
|
|
|
public $time; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Console messages |
79
|
|
|
* |
80
|
|
|
* @var array |
81
|
|
|
* @access public |
82
|
|
|
*/ |
83
|
|
|
public $console; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Session cookies |
87
|
|
|
* |
88
|
|
|
* @var array |
89
|
|
|
* @access public |
90
|
|
|
*/ |
91
|
|
|
public $cookies; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Import response data |
95
|
|
|
* |
96
|
|
|
* @access public |
97
|
|
|
* @param array $data |
98
|
|
|
* @return \JonnyW\PhantomJs\Http\Response |
99
|
|
|
*/ |
100
|
54 |
|
public function import(array $data) |
101
|
|
|
{ |
102
|
54 |
|
foreach ($data as $param => $value) { |
103
|
|
|
|
104
|
54 |
|
if ($param === 'headers') { |
105
|
36 |
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
52 |
|
if (property_exists($this, $param)) { |
109
|
52 |
|
$this->$param = $value; |
110
|
52 |
|
} |
111
|
54 |
|
} |
112
|
|
|
|
113
|
54 |
|
$this->headers = array(); |
114
|
|
|
|
115
|
54 |
|
if (isset($data['headers'])) { |
116
|
36 |
|
$this->setHeaders((array) $data['headers']); |
117
|
36 |
|
} |
118
|
|
|
|
119
|
54 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set headers array |
124
|
|
|
* |
125
|
|
|
* @access protected |
126
|
|
|
* @param array $headers |
127
|
|
|
* @return \JonnyW\PhantomJs\Http\Response |
128
|
|
|
*/ |
129
|
36 |
|
protected function setHeaders(array $headers) |
130
|
|
|
{ |
131
|
36 |
|
foreach ($headers as $header) { |
132
|
|
|
|
133
|
36 |
|
if (isset($header['name']) && isset($header['value'])) { |
134
|
36 |
|
$this->headers[$header['name']] = $header['value']; |
135
|
36 |
|
} |
136
|
36 |
|
} |
137
|
|
|
|
138
|
36 |
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get HTTP headers array |
143
|
|
|
* |
144
|
|
|
* @access public |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
2 |
|
public function getHeaders() |
148
|
|
|
{ |
149
|
2 |
|
return (array) $this->headers; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get HTTP header value for code |
154
|
|
|
* |
155
|
|
|
* @access public |
156
|
|
|
* @param string $code |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
2 |
|
public function getHeader($code) |
160
|
|
|
{ |
161
|
2 |
|
if (isset($this->headers[$code])) { |
162
|
1 |
|
return $this->headers[$code]; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get response status code |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
* @return integer |
173
|
|
|
*/ |
174
|
12 |
|
public function getStatus() |
175
|
|
|
{ |
176
|
12 |
|
return (int) $this->status; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get page content from response |
181
|
|
|
* |
182
|
|
|
* @access public |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
10 |
|
public function getContent() |
186
|
|
|
{ |
187
|
10 |
|
return $this->content; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get content type header |
192
|
|
|
* |
193
|
|
|
* @access public |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
1 |
|
public function getContentType() |
197
|
|
|
{ |
198
|
1 |
|
return $this->contentType; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Get request URL |
203
|
|
|
* |
204
|
|
|
* @access public |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
1 |
|
public function getUrl() |
208
|
|
|
{ |
209
|
1 |
|
return $this->url; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get redirect URL (if redirected) |
214
|
|
|
* |
215
|
|
|
* @access public |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
2 |
|
public function getRedirectUrl() |
219
|
|
|
{ |
220
|
2 |
|
return $this->redirectURL; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Is response a redirect |
225
|
|
|
* - Checks status codes |
226
|
|
|
* |
227
|
|
|
* @access public |
228
|
|
|
* @return boolean |
229
|
|
|
*/ |
230
|
9 |
|
public function isRedirect() |
231
|
|
|
{ |
232
|
9 |
|
$status = $this->getStatus(); |
233
|
|
|
|
234
|
9 |
|
return (bool) ($status >= 300 && $status <= 307); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get time string |
239
|
|
|
* |
240
|
|
|
* @access public |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
1 |
|
public function getTime() |
244
|
|
|
{ |
245
|
1 |
|
return $this->time; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get console messages |
250
|
|
|
* |
251
|
|
|
* @access public |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
2 |
|
public function getConsole() |
255
|
|
|
{ |
256
|
2 |
|
return $this->console; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get session cookies |
261
|
|
|
* |
262
|
|
|
* @access public |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
2 |
|
public function getCookies() |
266
|
|
|
{ |
267
|
2 |
|
return $this->cookies; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|