Managing a RESTful application in PHP can become a headache if you are new to REST but I have developed a reusable class function that will save you hours of development time shown below. Feel free to reuse it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php protected function returnRestfulValues($data) { if ($this->method == 'GET') { // find values in the url in fixed positions. ie: example/verb/123/456 return "SUCCESS: You performed a GET request using verb '" . $this->verb . "' with args " . $this->args[0]; } elseif ($this->method == 'POST' ) { // find values in POST request body. return $this->request; } elseif ($this->method == 'PUT') { // important! form 'enctype' should be: "x-www-form-urlencoded". // put url-encoded data contained in the request body into an array. // (ie: key/values from PUT request body (ie: "username=bob&password=willowtree") parse_str($this->file, $data); // http://php.net/manual/en/function.parse-str.php $url_values = $this->args; // (ie: the record ID to update) return $data; } elseif ($this->method == 'DELETE') { // works just like GET other than the HTTP verb DELETE return $this->args; } else { return "Whoops! you're not doing it correctly."; } } ?> |