1 | <?php |
||
8 | final class Response |
||
9 | { |
||
10 | public $statusCode; |
||
11 | public $headers; |
||
12 | public $body; |
||
13 | public $error; |
||
14 | private $jsonData; |
||
15 | public $duration; |
||
16 | |||
17 | /** @var array Mapping of status codes to reason phrases */ |
||
18 | private static $statusTexts = array( |
||
19 | 100 => 'Continue', |
||
20 | 101 => 'Switching Protocols', |
||
21 | 102 => 'Processing', |
||
22 | 200 => 'OK', |
||
23 | 201 => 'Created', |
||
24 | 202 => 'Accepted', |
||
25 | 203 => 'Non-Authoritative Information', |
||
26 | 204 => 'No Content', |
||
27 | 205 => 'Reset Content', |
||
28 | 206 => 'Partial Content', |
||
29 | 207 => 'Multi-Status', |
||
30 | 208 => 'Already Reported', |
||
31 | 226 => 'IM Used', |
||
32 | 300 => 'Multiple Choices', |
||
33 | 301 => 'Moved Permanently', |
||
34 | 302 => 'Found', |
||
35 | 303 => 'See Other', |
||
36 | 304 => 'Not Modified', |
||
37 | 305 => 'Use Proxy', |
||
38 | 307 => 'Temporary Redirect', |
||
39 | 308 => 'Permanent Redirect', |
||
40 | 400 => 'Bad Request', |
||
41 | 401 => 'Unauthorized', |
||
42 | 402 => 'Payment Required', |
||
43 | 403 => 'Forbidden', |
||
44 | 404 => 'Not Found', |
||
45 | 405 => 'Method Not Allowed', |
||
46 | 406 => 'Not Acceptable', |
||
47 | 407 => 'Proxy Authentication Required', |
||
48 | 408 => 'Request Timeout', |
||
49 | 409 => 'Conflict', |
||
50 | 410 => 'Gone', |
||
51 | 411 => 'Length Required', |
||
52 | 412 => 'Precondition Failed', |
||
53 | 413 => 'Request Entity Too Large', |
||
54 | 414 => 'Request-URI Too Long', |
||
55 | 415 => 'Unsupported Media Type', |
||
56 | 416 => 'Requested Range Not Satisfiable', |
||
57 | 417 => 'Expectation Failed', |
||
58 | 422 => 'Unprocessable Entity', |
||
59 | 423 => 'Locked', |
||
60 | 424 => 'Failed Dependency', |
||
61 | 425 => 'Reserved for WebDAV advanced collections expired proposal', |
||
62 | 426 => 'Upgrade required', |
||
63 | 428 => 'Precondition Required', |
||
64 | 429 => 'Too Many Requests', |
||
65 | 431 => 'Request Header Fields Too Large', |
||
66 | 500 => 'Internal Server Error', |
||
67 | 501 => 'Not Implemented', |
||
68 | 502 => 'Bad Gateway', |
||
69 | 503 => 'Service Unavailable', |
||
70 | 504 => 'Gateway Timeout', |
||
71 | 505 => 'HTTP Version Not Supported', |
||
72 | 506 => 'Variant Also Negotiates (Experimental)', |
||
73 | 507 => 'Insufficient Storage', |
||
74 | 508 => 'Loop Detected', |
||
75 | 510 => 'Not Extended', |
||
76 | 511 => 'Network Authentication Required', |
||
77 | ); |
||
78 | |||
79 | /** |
||
80 | * @param int $code 状态码 |
||
81 | * @param double $duration 请求时长 |
||
82 | * @param array $headers 响应头部 |
||
83 | * @param string $body 响应内容 |
||
84 | * @param string $error 错误描述 |
||
85 | */ |
||
86 | 102 | public function __construct($code, $duration, array $headers = array(), $body = null, $error = null) |
|
125 | |||
126 | 75 | public function json() |
|
130 | |||
131 | 90 | private static function bodyJson($body) |
|
135 | |||
136 | public function xVia() |
||
147 | |||
148 | 6 | public function xLog() |
|
152 | |||
153 | 6 | public function xReqId() |
|
157 | |||
158 | 81 | public function ok() |
|
162 | |||
163 | 6 | public function needRetry() |
|
164 | { |
||
165 | 6 | $code = $this->statusCode; |
|
166 | 6 | if ($code < 0 || ($code / 100 === 5 and $code !== 579) || $code === 996) { |
|
167 | 3 | return true; |
|
168 | } |
||
169 | 6 | } |
|
170 | |||
171 | 102 | private static function isJson($headers) |
|
176 | } |
||
177 |