Skip to content
This repository was archived by the owner on Jul 6, 2020. It is now read-only.
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ class Tools {
```


### Way 4. Automatic detect receive `Content-Type` as `application/json`, parse `php://input`

All data will be writed into `$_POST`

## Responses

The response body is always a array (JSON per default) containing a status code and the actual data.
Expand Down
23 changes: 23 additions & 0 deletions RestService/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class Server
public function __construct($pTriggerUrl, $pControllerClass = null, $pParentController = null)
{
$this->normalizeUrl($pTriggerUrl);
$this->detectContentType();

if ($pParentController) {
$this->parentController = $pParentController;
Expand Down Expand Up @@ -1205,4 +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
*/
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;
}
}