From e5a085ff484e79fbed1efd7c7209ee92525c0d3a Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 26 Nov 2012 12:45:39 -0500 Subject: [PATCH] Add first sample. Update PPP for one method. --- Request.inc.php | 2 +- samples/server-side-request-and-response.php | 46 ++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 samples/server-side-request-and-response.php diff --git a/Request.inc.php b/Request.inc.php index e9906ec..7e838ab 100644 --- a/Request.inc.php +++ b/Request.inc.php @@ -229,7 +229,7 @@ class Request { /** * Set instance members based on the HTTP request sent to the server. */ - protected function readHttpRequest() { + public function readHttpRequest() { $this->body = file_get_contents("php://input"); $this->headers = apache_request_headers(); diff --git a/samples/server-side-request-and-response.php b/samples/server-side-request-and-response.php new file mode 100644 index 0000000..f0c7e84 --- /dev/null +++ b/samples/server-side-request-and-response.php @@ -0,0 +1,46 @@ +readHttpRequest(); + +// Read some info from the request and store it to an associative array. +$rtn = array( + 'Path' => $rqst->path, + 'URI' => $rqst->uri, + 'Body' => $rqst->body, + 'Method' => $rqst->method, + 'Headers' => $rqst->headers +); + +// Create a new Response instance. +$resp = new \wellrested\Response(); + +// Set the status code to 200 OK. +$resp->statusCode = 200; + +// Set the content type for JSON. +$resp->setHeader('Content-Type', 'application/json'); + +// Add the associative array, encoded as JSON, as the body. +// (Note, setting the body automatically adds a Content-Length header.) +$resp->body = json_encode($rtn); + +// Output the response. +$resp->respond(); + +?> \ No newline at end of file