From ff7eb96e9595c2e77f590e24410f4e3098b361d1 Mon Sep 17 00:00:00 2001 From: Artem Kolesnik Date: Thu, 23 Jan 2014 14:31:15 +0400 Subject: [PATCH 1/3] Add parse php/input --- README.md | 18 ++++++++++++++++++ RestService/Server.php | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index 0f199f4..3f81981 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,24 @@ class Tools { ``` +### Way 4. Parse `php://input` + +For AngularJS you can need parse `php://input` as json + +`index.php`: + +```php + +use RestService\Server; + +Server::create('/admin', 'myRestApi\Admin') + ->collectRoutes() + ->parsePhpInput() +->run(); + +``` + + ## Responses The response body is always a array (JSON per default) containing a status code and the actual data. diff --git a/RestService/Server.php b/RestService/Server.php index da90476..3835027 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -1205,4 +1205,18 @@ public function findRoute($pUri, $pMethod = '_all_') return false; } + /** + * Parse data php://input to $_POST. It is used in AngularJS + * + * @return $this + */ + public function parsePhpInput(){ + $data = file_get_contents("php://input"); + $sData = json_decode($data, true); + if (!empty($sData)) foreach($sData as $key => $value) + $_POST[$key] = $value; + + return $this; + } + } From 7433703d9ffcf6df604dd5f746e9cfd0218fd02b Mon Sep 17 00:00:00 2001 From: Artem Kolesnik Date: Wed, 23 Jul 2014 13:02:06 +0400 Subject: [PATCH 2/3] Automatic detect & parse json over content-type --- RestService/Server.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/RestService/Server.php b/RestService/Server.php index 3835027..73ed037 100644 --- a/RestService/Server.php +++ b/RestService/Server.php @@ -139,6 +139,7 @@ class Server public function __construct($pTriggerUrl, $pControllerClass = null, $pParentController = null) { $this->normalizeUrl($pTriggerUrl); + $this->detectContentType(); if ($pParentController) { $this->parentController = $pParentController; @@ -1205,18 +1206,26 @@ public function findRoute($pUri, $pMethod = '_all_') return false; } + /** + * Detect content type request + */ + private function detectContentType(){ + if (!empty($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) { + $this->parsePhpInput(); + } + } + /** * Parse data php://input to $_POST. It is used in AngularJS * * @return $this */ - public function parsePhpInput(){ - $data = file_get_contents("php://input"); - $sData = json_decode($data, true); - if (!empty($sData)) foreach($sData as $key => $value) + private function parsePhpInput(){ + $input = file_get_contents("php://input"); + $data = json_decode($input, true); + if (!empty($data)) foreach($data as $key => $value) $_POST[$key] = $value; return $this; } - } From bbceb66cbec59f1373bb63d701f705bdd7466a29 Mon Sep 17 00:00:00 2001 From: Artem Kolesnik Date: Wed, 23 Jul 2014 13:07:55 +0400 Subject: [PATCH 3/3] Readme, detect content-type --- README.md | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3f81981..fb85f45 100644 --- a/README.md +++ b/README.md @@ -243,23 +243,9 @@ class Tools { ``` -### Way 4. Parse `php://input` - -For AngularJS you can need parse `php://input` as json - -`index.php`: - -```php - -use RestService\Server; - -Server::create('/admin', 'myRestApi\Admin') - ->collectRoutes() - ->parsePhpInput() -->run(); - -``` +### Way 4. Automatic detect receive `Content-Type` as `application/json`, parse `php://input` +All data will be writed into `$_POST` ## Responses