Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ added json support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,18 @@ public function getBody()
}
return $data;
}

/**
* Get json as associative array or object from request body
* Content-Type header should be application/json in HTTP POST
* @param boolean $associative When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects.
* @param integer $flags see https://www.php.net/manual/en/function.json-decode.php for different flags
* @param integer $depth Maximum nesting depth of the structure being decoded.
* @return mixed
*/
public function body($associative = true, int $flags = 0, int $depth = 512) {
$body = file_get_contents("php://input");
$object = json_decode($body, $associative, $depth, $flags);
return $object;
}
}
12 changes: 12 additions & 0 deletions Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public function redirect($url)
{
header("Location: $url");
}

/**
* Return json
*
* @param array $obj The associative array to return as json
* @param integer $flags see https://www.php.net/manual/en/function.json-encode.php for different flags
* @param integer $depth Set the maximum depth. Must be greater than zero.
* @return string
*/
public function json(array $obj, int $flags = 0, int $depth = 512) {
return json_encode($obj, $flags, $depth);
}
}