Example #1
0
 public static function processRequest()
 {
     // get our verb
     $request_method = strtolower($_SERVER['REQUEST_METHOD']);
     $response = new RestResponse();
     // we'll store our data here
     $data = array();
     switch ($request_method) {
         // gets are easy...
         case 'get':
             $data = $_GET;
             break;
             // so are posts
         // so are posts
         case 'post':
             $data = $_POST;
             break;
             // here's the tricky bit...
         // here's the tricky bit...
         case 'put':
             // basically, we read a string from PHP's special input location,
             // and then parse it out into an array via parse_str... per the PHP docs:
             // Parses str  as if it were the query string passed via a URL and sets
             // variables in the current scope.
             parse_str(file_get_contents('php://input'), $put_vars);
             $data = $put_vars;
             break;
         default:
             // TODO. Hvis vi når hertil bør vi nok råbe av.
             break;
     }
     if (isset($_GET['q'])) {
         $data['q'] = $_GET['q'];
     }
     // store the method
     $response->setMethod($request_method);
     // set the raw data, so we can access it if needed (there may be
     // other pieces to your requests)
     $response->setRequestVars($data);
     if (isset($data['data'])) {
         // TODO
         // translate the JSON to an Object for use however you want
         $response->setData(json_decode($data['data']));
     }
     $path = $response->getRequestVars();
     if (!isset($path['q'])) {
         die(RestUtils::sendResponse(404));
     }
     $path_array = explode('/', $path['q']);
     if (!isset($path_array[0])) {
         die(RestUtils::sendResponse(404));
     }
     // Set the controller.
     $controller = RestUtils::getController($response, $path_array);
     $controller->process();
     // TODO. Right now we only return json.
     $body = json_encode($controller->getProcessedResponse());
     RestUtils::sendResponse(200, $body, 'application/json');
 }