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