1010 */
1111class Response
1212{
13+ /**
14+ * Get http status code full text
15+ *
16+ * @param $statusCode
17+ * @return string
18+ */
19+ private function getStatusCodeMsg ($ statusCode )
20+ {
21+ $ msg = array (
22+ 100 => 'Continue ' ,
23+ 101 => 'Switching Protocols ' ,
24+ 102 => 'Processing ' ,
25+ 200 => 'OK ' ,
26+ 201 => 'Created ' ,
27+ 202 => 'Accepted ' ,
28+ 203 => 'Non-Authoritative Information ' ,
29+ 204 => 'No Content ' ,
30+ 205 => 'Reset Content ' ,
31+ 206 => 'Partial Content ' ,
32+ 207 => 'Multi-Status ' ,
33+ 208 => 'Already Reported ' ,
34+ 226 => 'IM Used ' ,
35+ 300 => 'Multiple Choices ' ,
36+ 301 => 'Moved Permanently ' ,
37+ 302 => 'Found ' ,
38+ 303 => 'See Other ' ,
39+ 304 => 'Not Modified ' ,
40+ 305 => 'Use Proxy ' ,
41+ 306 => 'Switch Proxy ' ,
42+ 307 => 'Temporary Redirect ' ,
43+ 308 => 'Permanent Redirect ' ,
44+ 400 => 'Bad Request ' ,
45+ 401 => 'Unauthorized ' ,
46+ 402 => 'Payment Required ' ,
47+ 403 => 'Forbidden ' ,
48+ 404 => 'Not Found ' ,
49+ 405 => 'Method Not Allowed ' ,
50+ 406 => 'Not Acceptable ' ,
51+ 407 => 'Proxy Authentication Required ' ,
52+ 408 => 'Request Timeout ' ,
53+ 409 => 'Conflict ' ,
54+ 410 => 'Gone ' ,
55+ 411 => 'Length Required ' ,
56+ 412 => 'Precondition Failed ' ,
57+ 413 => 'Request Entity Too Large ' ,
58+ 414 => 'Request-URI Too Long ' ,
59+ 415 => 'Unsupported Media Type ' ,
60+ 416 => 'Requested Range Not Satisfiable ' ,
61+ 417 => 'Expectation Failed ' ,
62+ 418 => 'I \'m a teapot ' ,
63+ 419 => 'Authentication Timeout ' ,
64+ 420 => 'Enhance Your Calm ' ,
65+ 422 => 'Unprocessable Entity ' ,
66+ 423 => 'Locked ' ,
67+ 424 => 'Failed Dependency ' ,
68+ 425 => 'Unordered Collection ' ,
69+ 426 => 'Upgrade Required ' ,
70+ 428 => 'Precondition Required ' ,
71+ 429 => 'Too Many Requests ' ,
72+ 431 => 'Request Header Fields Too Large ' ,
73+ 444 => 'No Response ' ,
74+ 449 => 'Retry With ' ,
75+ 450 => 'Blocked by Windows Parental Controls ' ,
76+ 451 => 'Unavailable For Legal Reasons ' ,
77+ 494 => 'Request Header Too Large ' ,
78+ 495 => 'Cert Error ' ,
79+ 496 => 'No Cert ' ,
80+ 497 => 'HTTP to HTTPS ' ,
81+ 499 => 'Client Closed Request ' ,
82+ 500 => 'Internal Server Error ' ,
83+ 501 => 'Not Implemented ' ,
84+ 502 => 'Bad Gateway ' ,
85+ 503 => 'Service Unavailable ' ,
86+ 504 => 'Gateway Timeout ' ,
87+ 505 => 'HTTP Version Not Supported ' ,
88+ 506 => 'Variant Also Negotiates ' ,
89+ 507 => 'Insufficient Storage ' ,
90+ 508 => 'Loop Detected ' ,
91+ 509 => 'Bandwidth Limit Exceeded ' ,
92+ 510 => 'Not Extended ' ,
93+ 511 => 'Network Authentication Required ' ,
94+ 598 => 'Network read timeout error ' ,
95+ 599 => 'Network connect timeout error ' ,
96+ );
97+
98+ return $ msg [$ statusCode ];
99+ }
100+
101+ /**
102+ * Set HTTP Headers
103+ *
104+ * @param $statusCode
105+ * @param $headers
106+ */
107+ private function setHTTPHeaders ($ statusCode , $ headers )
108+ {
109+ //Set HTTP Version and status header
110+ header ("{$ _SERVER ['SERVER_PROTOCOL ' ]} {$ statusCode } {$ this ->getStatusCodeMsg ($ statusCode )}" );
111+ //Set other headers
112+ foreach ($ headers as $ k => $ v )
113+ header ("{$ k }: $ v " );
114+ }
115+
116+ /**
117+ * Send Response.
118+ *
119+ * @param $content
120+ * @param int $statusCode
121+ * @param array $headers
122+ *
123+ * @return mixed
124+ */
125+ public function sendResponse ($ content , $ statusCode = 200 , $ headers = [])
126+ {
127+ if ($ content == '' || $ content == null )
128+ die ('Set response content! ' );
129+
130+ $ this ->setHTTPHeaders ($ statusCode , $ headers );
131+
132+ return $ content ;
133+ }
13134}
0 commit comments