From 6f252607898211298022e22570f986e2190711f3 Mon Sep 17 00:00:00 2001 From: aglr Date: Tue, 8 Mar 2016 11:47:45 +0100 Subject: [PATCH] Add JSON support to PUT requests If a PUT request is sent with the header "Content-Type: application/json" the input data will be json_decode as a POST request is decoded. (depends to the '$extended' parameter) --- Runtime.php | 55 ++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/Runtime.php b/Runtime.php index c37f88c..0f914c4 100644 --- a/Runtime.php +++ b/Runtime.php @@ -79,45 +79,48 @@ public static function getUri() */ public static function getData($extended = true) { - switch (static::getMethod()) { + $input = null; + $method = static::getMethod(); + $contentType = static::getHeader('Content-Type'); + + // map data + switch ($method) { case Request::METHOD_GET: - return $_GET; + $input = $_GET; + break; case Request::METHOD_POST: - $contentType = static::getHeader('Content-Type'); - - switch ($contentType) { - case 'application/x-www-form-urlencoded': - return $_POST; + $input = $_POST; + break; + } - case 'application/json': - $input = file_get_contents('php://input'); + switch ($contentType) { + case 'application/x-www-form-urlencoded': + if (null === $input) { + $content = file_get_contents('php://input'); + parse_str($content, $input); + } - if (true !== $extended || - true !== function_exists('json_decode')) { - return $input; - } + return $input; - $json = json_decode($input, true); + case 'application/json': + $input = file_get_contents('php://input'); - if (JSON_ERROR_NONE !== json_last_error()) { - return $input; - } + if (true !== $extended || + true !== function_exists('json_decode')) { + return $input; + } - return $json; + $json = json_decode($input, true); - default: - return file_get_contents('php://input'); + if (JSON_ERROR_NONE !== json_last_error()) { + return $input; } - break; - - case Request::METHOD_PUT: - case Request::METHOD_PATCH: - return file_get_contents('php://input'); + return $json; default: - return null; + return file_get_contents('php://input'); } }