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) |
|
87 | { |
||
88 | 102 | $this->statusCode = $code; |
|
89 | 102 | $this->duration = $duration; |
|
90 | 102 | $this->headers = $headers; |
|
91 | 102 | $this->body = $body; |
|
92 | 102 | $this->error = $error; |
|
93 | 102 | $this->jsonData = null; |
|
94 | 102 | if ($error !== null) { |
|
95 | 8 | return; |
|
96 | } |
||
97 | |||
98 | 94 | if ($body === null) { |
|
99 | if ($code >= 400) { |
||
100 | $this->error = self::$statusTexts[$code]; |
||
101 | } |
||
102 | return; |
||
103 | } |
||
104 | 94 | if (self::isJson($headers)) { |
|
105 | try { |
||
106 | 86 | $jsonData = self::bodyJson($body); |
|
107 | 86 | if ($code >= 400) { |
|
108 | 21 | $this->error = $body; |
|
109 | 21 | if ($jsonData['error'] !== null) { |
|
110 | 21 | $this->error = $jsonData['error']; |
|
111 | 21 | } |
|
112 | 21 | } |
|
113 | 86 | $this->jsonData = $jsonData; |
|
114 | 86 | } catch (\InvalidArgumentException $e) { |
|
115 | $this->error = $body; |
||
116 | if ($code >= 200 && $code < 300) { |
||
117 | $this->error = $e->getMessage(); |
||
118 | } |
||
119 | } |
||
120 | 94 | } elseif ($code >= 400) { |
|
121 | 1 | $this->error = $body; |
|
122 | 1 | } |
|
123 | 94 | return; |
|
124 | } |
||
125 | |||
126 | 72 | public function json() |
|
130 | |||
131 | 86 | private static function bodyJson($body) |
|
135 | |||
136 | public function xVia() |
||
147 | |||
148 | 5 | public function xLog() |
|
152 | |||
153 | 5 | public function xReqId() |
|
157 | |||
158 | 81 | public function ok() |
|
162 | |||
163 | 6 | public function needRetry() |
|
170 | |||
171 | 94 | private static function isJson($headers) |
|
176 | } |
||
177 |