Updates to Request class, including adding the request() method for cURL access to resources.

This commit is contained in:
PJ Dietz 2012-11-27 16:57:27 -05:00
parent 66be033022
commit d168265700
2 changed files with 174 additions and 19 deletions

View File

@ -2,6 +2,8 @@
namespace wellrested; namespace wellrested;
require_once(dirname(__FILE__) . '/Response.inc.php');
/** /**
* A Request instance contains information relating to the current HTTP request * A Request instance contains information relating to the current HTTP request
* a client sent to the server. * a client sent to the server.
@ -35,6 +37,13 @@ class Request {
*/ */
protected $headers; protected $headers;
/**
* The Hostname for the request (e.g., www.google.com)
*
* @var string
*/
protected $hostname;
/** /**
* HTTP method or verb for the request * HTTP method or verb for the request
* *
@ -47,7 +56,7 @@ class Request {
* *
* @var string * @var string
*/ */
protected $path; protected $path = '/';
/** /**
* Array of fragments of the path, delimited by slashes * Array of fragments of the path, delimited by slashes
@ -56,6 +65,13 @@ class Request {
*/ */
protected $pathParts; protected $pathParts;
/**
* Protocal for the request (e.g., http, https)
*
* @var string
*/
protected $protocol = 'http';
/** /**
* Associative array of query parameters * Associative array of query parameters
* *
@ -64,11 +80,12 @@ class Request {
protected $query; protected $query;
/** /**
* The full URI of the request * The string value of the full URI. This is reconstructed from the
* components if requested when unset.
* *
* @var string * @var string
*/ */
protected $uri; protected $uri = null;
/** /**
* Singleton instance derived from reading info from Apache. * Singleton instance derived from reading info from Apache.
@ -94,12 +111,16 @@ class Request {
return $this->getBody(); return $this->getBody();
case 'headers': case 'headers':
return $this->getHeaders(); return $this->getHeaders();
case 'hostname':
return $this->getHostname();
case 'method': case 'method':
return $this->getMethod(); return $this->getMethod();
case 'path': case 'path':
return $this->getPath(); return $this->getPath();
case 'pathParts': case 'pathParts':
return $this->getPathParts(); return $this->getPathParts();
case 'protocol':
return $this->getProtocol();
case 'query': case 'query':
return $this->getQuery(); return $this->getQuery();
case 'uri': case 'uri':
@ -118,9 +139,21 @@ class Request {
public function __set($name, $value) { public function __set($name, $value) {
switch ($name) { switch ($name) {
case 'body':
$this->setBody($value);
return;
case 'hostname':
$this->setHostname($value);
return;
case 'method':
$this->setMethod($value);
return;
case 'path': case 'path':
$this->setPath($value); $this->setPath($value);
return; return;
case 'protocol':
$this->setProtocol($value);
return;
case 'query': case 'query':
$this->setQuery($value); $this->setQuery($value);
return; return;
@ -141,10 +174,18 @@ class Request {
return $this->headers; return $this->headers;
} }
public function getHostname() {
return $this->hostname;
}
public function getMethod() { public function getMethod() {
return $this->method; return $this->method;
} }
public function getProtocol() {
return $this->rotocol;
}
public function getPath() { public function getPath() {
return $this->path; return $this->path;
} }
@ -158,7 +199,37 @@ class Request {
} }
public function getUri() { public function getUri() {
// Construct the URI if it is unset.
if (!is_null($this->uri)) {
$this->rebuildUri();
}
return $this->uri; return $this->uri;
}
/**
* Set the body for the request.
*
* @param string $body
*/
public function setBody($body) {
$this->body = $body;
}
/**
* Set the hostname for the request and update the URI.
*
* @param string $hostname
*/
public function setHostname($hostname) {
$this->hostname = $hostname;
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -174,6 +245,7 @@ class Request {
} }
$this->method = $method; $this->method = $method;
} }
/** /**
@ -186,6 +258,22 @@ class Request {
$this->path = $path; $this->path = $path;
$this->pathParts = explode('/', substr($path, 1)); $this->pathParts = explode('/', substr($path, 1));
// Update the URI member.
$this->rebuildUri();
}
/**
* Set the protocol for the request and update the URI.
*
* @param string $protocol
*/
public function setProtocol($protocol) {
$this->protocol = $protocol;
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -205,6 +293,9 @@ class Request {
throw new \InvalidArgumentException('Unable to parse query string.'); throw new \InvalidArgumentException('Unable to parse query string.');
} }
// Update the URI member.
$this->rebuildUri();
} }
/** /**
@ -215,21 +306,12 @@ class Request {
*/ */
public function setUri($uri) { public function setUri($uri) {
/*
* TODO, eventually do all of these:
http
host
port
user
pass
path
query - after the question mark ?
fragment - after the hashmark #
*/
$this->uri = $uri; $this->uri = $uri;
$parsed = parse_url($uri); $parsed = parse_url($uri);
$host = isset($parsed['host']) ? $parsed['host'] : '';
$this->setHostname($host);
$path = isset($parsed['path']) ? $parsed['path'] : ''; $path = isset($parsed['path']) ? $parsed['path'] : '';
$this->setPath($path); $this->setPath($path);
@ -239,8 +321,74 @@ class Request {
} }
/**
* Build the URI member from the other members (path, query, etc.)
*/
protected function rebuildUri() {
$uri = $this->protocol . '://' . $this->hostname . $this->path;
if ($this->query) {
$uri .= '?' . http_build_query($this->query);
}
$this->uri = $uri;
}
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
public function request() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
switch ($this->method) {
case 'GET':
curl_setopt($ch, CURLOPT_HTTPGET, 1);
break;
case 'POST':
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
break;
case 'PUT':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
break;
}
$result = curl_exec($ch);
$resp = new Response();
if ($result !== false) {
$resp->body = $result;
$resp->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// TODO: Read headers
}
// TODO: Account for error
curl_close($ch);
return $resp;
}
/** /**
* Set instance members based on the HTTP request sent to the server. * Set instance members based on the HTTP request sent to the server.
*/ */
@ -249,7 +397,8 @@ class Request {
$this->body = file_get_contents("php://input"); $this->body = file_get_contents("php://input");
$this->headers = apache_request_headers(); $this->headers = apache_request_headers();
$this->method = $_SERVER['REQUEST_METHOD']; $this->method = $_SERVER['REQUEST_METHOD'];
$this->setUri($_SERVER['REQUEST_URI']); $this->uri = $_SERVER['REQUEST_URI'];
$this->hostname = $_SERVER['HTTP_HOST'];
} // readHttpRequest() } // readHttpRequest()

View File

@ -11,14 +11,20 @@
require_once('../Request.inc.php'); require_once('../Request.inc.php');
require_once('../Response.inc.php'); require_once('../Response.inc.php');
$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. // Set some of the information for it.
$rqst->path = 'https://www.google.com/search'; $rqst->hostname = $thisRequest->hostname;
$rqst->query = array('q' => 'rest api'); $rqst->path = '/wellrested/samples/server-side-request-and-response.php';
$rqst->method = 'PUT';
$rqst->body = 'This is the body';
print $rqst->uri; $resp = $rqst->request();
print 'Response code: ' . $resp->statusCode . "\n";
print 'Response body: ' . $resp->body . "\n";
?> ?>