parent
0052ecd455
commit
34ed07f4a0
114
Request.inc.php
114
Request.inc.php
|
|
@ -3,6 +3,9 @@
|
||||||
namespace wellrested;
|
namespace wellrested;
|
||||||
|
|
||||||
require_once(dirname(__FILE__) . '/Response.inc.php');
|
require_once(dirname(__FILE__) . '/Response.inc.php');
|
||||||
|
require_once(dirname(__FILE__) . '/exceptions/CurlException.inc.php');
|
||||||
|
|
||||||
|
// !TODO: Include port in the URI
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Request instance contains information relating to the current HTTP request
|
* A Request instance contains information relating to the current HTTP request
|
||||||
|
|
@ -14,10 +17,12 @@ require_once(dirname(__FILE__) . '/Response.inc.php');
|
||||||
*
|
*
|
||||||
* @property string body Entity body of the request
|
* @property string body Entity body of the request
|
||||||
* @property array headers Associative array of HTTP headers
|
* @property array headers Associative array of HTTP headers
|
||||||
|
* @property array hostname The Hostname for the request (e.g., google.com)
|
||||||
* @property string method HTTP method or verb for the request
|
* @property string method HTTP method or verb for the request
|
||||||
* @property string path Path component of the URI for the request
|
* @property string path Path component of the URI for the request
|
||||||
* @property string pathParts Fragments of the path, delimited by slashes
|
* @property string pathParts Fragments of the path, delimited by slashes
|
||||||
* @property array query Associative array of query parameters
|
* @property array query Associative array of query parameters
|
||||||
|
* @property array uri Full URI, including protocol, hostname, path, and query
|
||||||
*
|
*
|
||||||
* @package WellRESTed
|
* @package WellRESTed
|
||||||
*/
|
*/
|
||||||
|
|
@ -49,7 +54,7 @@ class Request {
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $method;
|
protected $method = 'GET';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path component of the URI for the request
|
* Path component of the URI for the request
|
||||||
|
|
@ -166,38 +171,83 @@ class Request {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the body payload of the instance.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getBody() {
|
public function getBody() {
|
||||||
return $this->body;
|
return $this->body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an associative array of all set headers.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getHeaders() {
|
public function getHeaders() {
|
||||||
return $this->headers;
|
return $this->headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the hostname set for the instance.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getHostname() {
|
public function getHostname() {
|
||||||
return $this->hostname;
|
return $this->hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the HTTP method (e.g., GET, POST, PUT, DELETE)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getMethod() {
|
public function getMethod() {
|
||||||
return $this->method;
|
return $this->method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the protocol (e.g., http, https)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getProtocol() {
|
public function getProtocol() {
|
||||||
return $this->rotocol;
|
return $this->protocol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the path part of the URI as a string.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getPath() {
|
public function getPath() {
|
||||||
return $this->path;
|
return $this->path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array of the sections of the path delimited by slashes.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getPathParts() {
|
public function getPathParts() {
|
||||||
return $this->pathParts;
|
return $this->pathParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an associative array representing the query.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getQuery() {
|
public function getQuery() {
|
||||||
return $this->query;
|
return $this->query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the full URI includeing protocol, hostname, path, and query.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function getUri() {
|
public function getUri() {
|
||||||
|
|
||||||
// Construct the URI if it is unset.
|
// Construct the URI if it is unset.
|
||||||
|
|
@ -217,7 +267,6 @@ class Request {
|
||||||
$this->body = $body;
|
$this->body = $body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the hostname for the request and update the URI.
|
* Set the hostname for the request and update the URI.
|
||||||
*
|
*
|
||||||
|
|
@ -320,7 +369,6 @@ class Request {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the URI member from the other members (path, query, etc.)
|
* Build the URI member from the other members (path, query, etc.)
|
||||||
*/
|
*/
|
||||||
|
|
@ -336,15 +384,28 @@ class Request {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make a cURL request out of the instance and return a Response.
|
||||||
|
*
|
||||||
|
* @return Response
|
||||||
|
* @throws exceptions\CurlException
|
||||||
|
*/
|
||||||
public function request() {
|
public function request() {
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
|
|
||||||
|
// Set the URL.
|
||||||
curl_setopt($ch, CURLOPT_URL, $this->uri);
|
curl_setopt($ch, CURLOPT_URL, $this->uri);
|
||||||
|
|
||||||
|
// Include headers in the response.
|
||||||
|
curl_setopt($ch, CURLOPT_HEADER, 1);
|
||||||
|
|
||||||
|
// Return the response from curl_exec().
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
|
||||||
|
// Set the method. Include the body, if needed.
|
||||||
switch ($this->method) {
|
switch ($this->method) {
|
||||||
|
|
||||||
case 'GET':
|
case 'GET':
|
||||||
|
|
@ -368,20 +429,35 @@ class Request {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make the cURL request.
|
||||||
$result = curl_exec($ch);
|
$result = curl_exec($ch);
|
||||||
|
|
||||||
$resp = new Response();
|
// Throw an exception in the event of a cURL error.
|
||||||
|
if ($result === false) {
|
||||||
if ($result !== false) {
|
$error = curl_error($ch);
|
||||||
|
$errno = curl_errno($ch);
|
||||||
$resp->body = $result;
|
curl_close($ch);
|
||||||
$resp->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
throw new exceptions\CurlException($error, $errno);
|
||||||
|
|
||||||
// TODO: Read headers
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Account for error
|
// Make a reponse to populate and return with data obtained via cURL.
|
||||||
|
$resp = new Response();
|
||||||
|
|
||||||
|
$resp->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
// Split the result into headers and body.
|
||||||
|
list ($headers, $body) = explode("\r\n\r\n", $result, 2);
|
||||||
|
|
||||||
|
// Set the body. Do not auto-add the Content-length header.
|
||||||
|
$resp->setBody($body, false);
|
||||||
|
|
||||||
|
// Iterate over the headers line by line and add each one.
|
||||||
|
foreach (explode("\r\n", $headers) as $header) {
|
||||||
|
if (strpos($header, ':')) {
|
||||||
|
list ($headerName, $headerValue) = explode(':', $header, 2);
|
||||||
|
$resp->setHeader($headerName, ltrim($headerValue));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
|
|
@ -394,13 +470,13 @@ class Request {
|
||||||
*/
|
*/
|
||||||
public function readHttpRequest() {
|
public function readHttpRequest() {
|
||||||
|
|
||||||
$this->body = file_get_contents("php://input");
|
$this->setBody(file_get_contents("php://input"), false);
|
||||||
$this->headers = apache_request_headers();
|
$this->headers = apache_request_headers();
|
||||||
$this->method = $_SERVER['REQUEST_METHOD'];
|
$this->method = $_SERVER['REQUEST_METHOD'];
|
||||||
$this->uri = $_SERVER['REQUEST_URI'];
|
$this->uri = $_SERVER['REQUEST_URI'];
|
||||||
$this->hostname = $_SERVER['HTTP_HOST'];
|
$this->hostname = $_SERVER['HTTP_HOST'];
|
||||||
|
|
||||||
} // readHttpRequest()
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a reference to the singleton instance of the Request derived
|
* Return a reference to the singleton instance of the Request derived
|
||||||
|
|
@ -409,7 +485,7 @@ class Request {
|
||||||
* @return Request
|
* @return Request
|
||||||
* @static
|
* @static
|
||||||
*/
|
*/
|
||||||
static public function getRequest() {
|
public static function getRequest() {
|
||||||
|
|
||||||
if (!isset(self::$theRequest)) {
|
if (!isset(self::$theRequest)) {
|
||||||
|
|
||||||
|
|
@ -423,7 +499,7 @@ class Request {
|
||||||
|
|
||||||
return self::$theRequest;
|
return self::$theRequest;
|
||||||
|
|
||||||
} // getRequest()
|
}
|
||||||
|
|
||||||
} // Request
|
} // Request
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the file that is requested by client-set-request-and-response.php
|
||||||
|
*
|
||||||
|
* Feel free to modify this script, then run client-set-request-and-response.php
|
||||||
|
* to see the results.
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once('../Response.inc.php');
|
||||||
|
|
||||||
|
// Create a new Response instance.
|
||||||
|
$resp = new \wellrested\Response();
|
||||||
|
$resp->statusCode = 200;
|
||||||
|
$resp->setHeader('Content-Type', 'text/plain');
|
||||||
|
$resp->setHeader('User-Agent', 'Well RESTed');
|
||||||
|
$resp->body = 'The test works!';
|
||||||
|
$resp->respond();
|
||||||
|
exit;
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -5,26 +5,54 @@
|
||||||
*
|
*
|
||||||
* This script will build a request to an external server, issue the request,
|
* This script will build a request to an external server, issue the request,
|
||||||
* then read the reponse returned by the server.
|
* then read the reponse returned by the server.
|
||||||
|
*
|
||||||
|
* Please modify samples/client-side-endpoint.php to see results.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Include the Well RESTed Request and Response class files.
|
// Include the Well RESTed Request and Response class files.
|
||||||
require_once('../Request.inc.php');
|
require_once('../Request.inc.php');
|
||||||
require_once('../Response.inc.php');
|
require_once('../Response.inc.php');
|
||||||
|
|
||||||
|
// Get a Request instance describing the request made to this script.
|
||||||
$thisRequest = \wellrested\Request::getRequest();
|
$thisRequest = \wellrested\Request::getRequest();
|
||||||
|
|
||||||
// Create a new empty request.
|
// Create a new empty request.
|
||||||
$rqst = new \wellrested\Request();
|
$rqst = new \wellrested\Request();
|
||||||
|
|
||||||
// Set some of the information for it.
|
|
||||||
$rqst->hostname = $thisRequest->hostname;
|
$rqst->hostname = $thisRequest->hostname;
|
||||||
$rqst->path = '/wellrested/samples/server-side-request-and-response.php';
|
$rqst->path = '/wellrested/samples/client-side-endpoint.php';
|
||||||
$rqst->method = 'PUT';
|
|
||||||
$rqst->body = 'This is the body';
|
|
||||||
|
|
||||||
$resp = $rqst->request();
|
// Uncomment this to get a cURL exception.
|
||||||
|
//$rqst->uri = 'http://not-a-real.domain';
|
||||||
|
|
||||||
print 'Response code: ' . $resp->statusCode . "\n";
|
// Issue the request, and read the response returned by the server.
|
||||||
print 'Response body: ' . $resp->body . "\n";
|
try {
|
||||||
|
$resp = $rqst->request();
|
||||||
|
} catch (\wellrested\exceptions\CurlException $e) {
|
||||||
|
|
||||||
|
// Create new response to send to output to the browser.
|
||||||
|
$myResponse = new \wellrested\Response();
|
||||||
|
$myResponse->statusCode = 500;
|
||||||
|
$myResponse->setHeader('Content-Type', 'text/plain');
|
||||||
|
$myResponse->body = 'Message: ' .$e->getMessage() ."\n";
|
||||||
|
$myResponse->body .= 'Code: ' . $e->getCode() . "\n";
|
||||||
|
$myResponse->respond();
|
||||||
|
exit;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new response to send to output to the browser.
|
||||||
|
$myResponse = new \wellrested\Response();
|
||||||
|
$myResponse->statusCode = 200;
|
||||||
|
$myResponse->setHeader('Content-Type', 'application/json');
|
||||||
|
|
||||||
|
$json = array(
|
||||||
|
'Status Code' => $resp->statusCode,
|
||||||
|
'Body' => $resp->body,
|
||||||
|
'Headers' => $resp->headers
|
||||||
|
);
|
||||||
|
$myResponse->body = json_encode($json);
|
||||||
|
|
||||||
|
$myResponse->respond();
|
||||||
|
exit;
|
||||||
|
|
||||||
?>
|
?>
|
||||||
Loading…
Reference in New Issue