3 declare(strict_types=1);
66 $this->requestmethod =
$_SERVER[
'REQUEST_METHOD'];
68 $this->contenttype =
'';
71 $this->responsecodes = array(
75 300 =>
'Multiple Choices',
77 401 =>
'Unauthorized',
78 402 =>
'Payment Required',
81 405 =>
'Method Not Allowed',
82 406 =>
'Not Acceptable',
83 415 =>
'Unsupported Media Type',
84 500 =>
'Internal Server Error',
85 501 =>
'Not Implemented' 87 $this->additionalheaders = array();
113 if (($code < 200) || ($code >= 600)) {
116 if (empty($this->reason) && array_key_exists($code, $this->responsecodes)) {
117 $this->reason = $this->responsecodes[
$code];
120 if (empty($this->reason)) {
121 $this->reason = $this->responsecodes[intval($code / 100) * 100];
209 public function send($debug =
true): void
211 header(
"HTTP/1.0 {$this->code} {$this->getReason()}");
212 foreach ($this->additionalheaders as $header) {
216 if ($this->code >= 200 && $this->code < 400) {
222 if ((($this->code >= 200) && ($this->code < 300)) || !empty($this->body)) {
223 if (!empty($this->contenttype)) {
224 header(
"Content-Type: $this->contenttype; charset=utf-8");
226 if (!empty($this->body)) {
229 } elseif ($this->code >= 400) {
230 header(
"Content-Type: application/json; charset=utf-8");
235 $body->request->method =
$_SERVER[
'REQUEST_METHOD'];
236 $body->request->url =
$_SERVER[
'REQUEST_URI'];
237 if (isset(
$_SERVER[
'HTTP_ACCEPT'])) {
238 $body->request->accept =
$_SERVER[
'HTTP_ACCEPT'];
240 if (isset(
$_SERVER[
'CONTENT_TYPE'])) {
241 $body->request->contentType = explode(
';',
$_SERVER[
'CONTENT_TYPE'], 2)[0];
243 echo json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
setBody(string $body)
Set the response body.
array $responsecodes
HTTP response codes.
string $body
HTTP response body.
__construct()
Class constructor.
getAccept()
Get the request accept header.
string $accept
HTTP request accept header.
getReason()
Get the response reason.
getRequestMethod()
Get the request method.
int $code
HTTP response code.
getRequestData()
Get the request body.
string $data
HTTP request body.
getCode()
Get the response code.
array $additionalheaders
HTTP additional headers.
getContentType()
Get the response content type.
setCode(int $code)
Set the response code.
setReason(string $reason)
Set the response reason.
send($debug=true)
Add an additional header.
string $reason
HTTP response reason.
setAccept(string $accept)
Set the request accept header.
setRequestData(string $data)
Set the response body.
string $requestmethod
HTTP request method.
setContentType(string $contenttype)
Set the response content type.
string $contenttype
HTTP response content type.