1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine HTTP |
5
|
|
|
* |
6
|
|
|
* Platine HTTP Message is the implementation of PSR 7 |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine HTTP |
11
|
|
|
* Copyright (c) 2011 - 2017 rehyved.com |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file HttpResponse.php |
34
|
|
|
* |
35
|
|
|
* The Http Response class |
36
|
|
|
* |
37
|
|
|
* @package Platine\Http\Client |
38
|
|
|
* @author Platine Developers team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\Http\Client; |
49
|
|
|
|
50
|
|
|
use Platine\Stdlib\Helper\Json; |
51
|
|
|
use Platine\Stdlib\Helper\Xml; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @class HttpResponse |
55
|
|
|
* @package Platine\Http\Client |
56
|
|
|
*/ |
57
|
|
|
class HttpResponse |
58
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* The request URL |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected string $url; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The HTTP status code |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
protected int $statusCode; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* The HTTP headers size |
73
|
|
|
* @var int |
74
|
|
|
*/ |
75
|
|
|
protected int $headerSize = 0; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The response headers |
79
|
|
|
* @var array<string, mixed> |
80
|
|
|
*/ |
81
|
|
|
protected array $headers = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The response cookies |
85
|
|
|
* @var array<string, HttpCookie> |
86
|
|
|
*/ |
87
|
|
|
protected array $cookies = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* The content type |
91
|
|
|
* @var string |
92
|
|
|
*/ |
93
|
|
|
protected string $contentType = ''; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The response content length |
97
|
|
|
* @var int |
98
|
|
|
*/ |
99
|
|
|
protected int $contentLength = 0; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The response raw content |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
protected string $content = ''; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The response error |
109
|
|
|
* @var string |
110
|
|
|
*/ |
111
|
|
|
protected string $error = ''; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create new instance |
115
|
|
|
* @param array<string, mixed> $requestInfo |
116
|
|
|
* @param array<string, mixed> $headers |
117
|
|
|
* @param mixed $response |
118
|
|
|
* @param string $error |
119
|
|
|
*/ |
120
|
|
|
public function __construct( |
121
|
|
|
array $requestInfo, |
122
|
|
|
array $headers, |
123
|
|
|
$response, |
124
|
|
|
string $error |
125
|
|
|
) { |
126
|
|
|
$this->url = $requestInfo['url']; |
127
|
|
|
$this->statusCode = (int) $requestInfo['http_code']; |
128
|
|
|
$this->headers = $headers; |
129
|
|
|
$this->cookies = $this->processCookies($headers); |
130
|
|
|
|
131
|
|
|
if ($response !== false) { |
132
|
|
|
$this->contentType = $requestInfo['content_type']; |
133
|
|
|
$this->headerSize = (int) $requestInfo['header_size']; |
134
|
|
|
$this->contentLength = (int) ($requestInfo['content_length'] ?? (strlen($response) - $this->headerSize)); |
135
|
|
|
$this->content = $this->processContent($response, $this->headerSize); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->error = $error; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return the request URL |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getUrl(): string |
146
|
|
|
{ |
147
|
|
|
return $this->url; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return the HTTP status code |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
|
|
public function getStatusCode(): int |
155
|
|
|
{ |
156
|
|
|
return $this->statusCode; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return the error message |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function getError(): string |
164
|
|
|
{ |
165
|
|
|
return $this->error; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return the header size |
171
|
|
|
* @return int |
172
|
|
|
*/ |
173
|
|
|
public function getHeaderSize(): int |
174
|
|
|
{ |
175
|
|
|
return $this->headerSize; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return the headers |
180
|
|
|
* @return array<string, mixed> |
181
|
|
|
*/ |
182
|
|
|
public function getHeaders(): array |
183
|
|
|
{ |
184
|
|
|
return $this->headers; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Return the HTTP header |
189
|
|
|
* @param string $name |
190
|
|
|
* @return mixed|null |
191
|
|
|
*/ |
192
|
|
|
public function getHeader(string $name) |
193
|
|
|
{ |
194
|
|
|
return $this->headers[$name] ?? null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return the cookies |
199
|
|
|
* @return array<string, HttpCookie> |
200
|
|
|
*/ |
201
|
|
|
public function getCookies(): array |
202
|
|
|
{ |
203
|
|
|
return $this->cookies; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Return the HTTP Cookie |
208
|
|
|
* @param string $name |
209
|
|
|
* @return HttpCookie|null |
210
|
|
|
*/ |
211
|
|
|
public function getCookie(string $name): ?HttpCookie |
212
|
|
|
{ |
213
|
|
|
return $this->cookies[$name] ?? null; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return the response content type |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function getContentType(): string |
221
|
|
|
{ |
222
|
|
|
return $this->contentType; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Return the response content length |
227
|
|
|
* @return int |
228
|
|
|
*/ |
229
|
|
|
public function getContentLength(): int |
230
|
|
|
{ |
231
|
|
|
return $this->contentLength; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Return the raw response content |
236
|
|
|
* @return string |
237
|
|
|
*/ |
238
|
|
|
public function getContent(): string |
239
|
|
|
{ |
240
|
|
|
return $this->content; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Return the response as JSON |
245
|
|
|
* @param bool $assoc |
246
|
|
|
* @param int $depth |
247
|
|
|
* @param int $options |
248
|
|
|
* @return array<mixed>|object |
249
|
|
|
*/ |
250
|
|
|
public function json( |
251
|
|
|
bool $assoc = false, |
252
|
|
|
int $depth = 512, |
253
|
|
|
int $options = 0 |
254
|
|
|
) { |
255
|
|
|
return Json::decode($this->content, $assoc, $depth, $options); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Return the response as XML |
260
|
|
|
* @return mixed |
261
|
|
|
*/ |
262
|
|
|
public function xml() |
263
|
|
|
{ |
264
|
|
|
return Xml::decode($this->content); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Whether the HTTP response indicated success or failure |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function isError(): bool |
272
|
|
|
{ |
273
|
|
|
return !empty($this->error) || HttpStatus::isError($this->statusCode); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Process the cookies headers |
279
|
|
|
* @param array<string, string[]> $headers |
280
|
|
|
* @return array<string, HttpCookie> |
281
|
|
|
*/ |
282
|
|
|
protected function processCookies(array $headers): array |
283
|
|
|
{ |
284
|
|
|
$cookies = []; |
285
|
|
|
foreach ($headers as $header => $value) { |
286
|
|
|
if (stripos($header, 'Set-Cookie') !== false) { |
287
|
|
|
foreach ($value as $cookieString) { |
288
|
|
|
$cookie = new HttpCookie($cookieString); |
289
|
|
|
$cookies[$cookie->getName()] = $cookie; |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $cookies; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Process the response content |
299
|
|
|
* @param string $response |
300
|
|
|
* @param int $headerSize |
301
|
|
|
* @return string |
302
|
|
|
*/ |
303
|
|
|
protected function processContent(string $response, int $headerSize): string |
304
|
|
|
{ |
305
|
|
|
return substr($response, $headerSize); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|