Request
in package
A class to encapsulate a HTTP request.
Tags
Table of Contents
Properties
- $body : string
- The request body if one was provided.
- $cookies : array<string|int, mixed>
- An associative array of HTTP cookies on this request.
- $files : array<string|int, mixed>
- An associative 3D array of uploaded files.
- $headers : array<string|int, mixed>
- An associative array of HTTP headers on this request.
- $host : string
- The host header provided on the request.
- $HTTPMethods : array<string|int, mixed>
- Array of supported HTTP methods.
- $IP : string
- The IP of the client making the request.
- $method : string
- The HTTP method of this request (must be in HTTPMethods array).
- $params : array<string|int, mixed>
- The HTTP params (form data and query string) on this request.
- $queryString : string
- The query string provided on the request (if any).
- $URI : string
- The URI requested.
Methods
- __construct() : mixed
- Builds up the request based on available PHP super globals, in addition to any overrides provided (useful for testing).
- addParams() : void
- Append the hash array provided to the params for this request.
- getAccept() : string|null
- Get the Accept header of the request.
- getBody() : string
- Get the request body if one was provided.
- getContentLength() : string
- Get the Content-Length header of the request.
- getContentType() : string|null
- Get the Content-Type header of the request.
- getCookie() : mixed
- Get the cookie matching the key provided.
- getCookies() : array<string|int, mixed>
- Return all cookies on this request.
- getFile() : mixed
- Get the file matching the key provided.
- getFiles() : array<string|int, mixed>
- Return all files on this request.
- getHeader() : string|null
- Get the header matching the key provided.
- getHeaders() : array<string|int, mixed>
- Return all headers on this request.
- getHost() : string
- Get the host name of the client that sent the request.
- getIP() : string
- Get the IP address of the client that sent the request.
- getMethod() : string
- Get the HTTP method of this request.
- getParam() : string|null
- Get the param matching the key provided.
- getParams() : array<string|int, mixed>
- Return all params on this request.
- getQueryString() : string
- Get the query string provided on the request.
- getReferrer() : string
- Get the Referrer header of the request.
- getURI() : string
- Get the URI that was requested.
- getURL() : string
- Get the URL that was requested.
- getUserAgent() : string|null
- Get the User-Agent header of the request.
- isSecureURI() : bool
- Checks to see if the request contains a secure/encrypted token.
- parseParamsFromRoute() : void
- Parses the route provided to extract matching params of the route from this request's URI.
- setMethod() : void
- Set the HTTP method of this request.
- setParams() : void
- Set the params array.
- getGlobalBody() : string
- Attempts to get the raw body of the current request from super globals.
- getGlobalHeaders() : array<string|int, mixed>
- Tries to get the current HTTP request headers from super globals.
- getGlobalServerValue() : string
- Tries to get the requested param from the $_SERVER super global, otherwise returns an empty string.
Properties
$body
The request body if one was provided.
private
string
$body
Tags
$cookies
An associative array of HTTP cookies on this request.
private
array<string|int, mixed>
$cookies
Tags
$files
An associative 3D array of uploaded files.
private
array<string|int, mixed>
$files
Tags
$headers
An associative array of HTTP headers on this request.
private
array<string|int, mixed>
$headers
Tags
$host
The host header provided on the request.
private
string
$host
Tags
$HTTPMethods
Array of supported HTTP methods.
private
array<string|int, mixed>
$HTTPMethods
= array('HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE')
Tags
$IP
The IP of the client making the request.
private
string
$IP
Tags
$method
The HTTP method of this request (must be in HTTPMethods array).
private
string
$method
Tags
$params
The HTTP params (form data and query string) on this request.
private
array<string|int, mixed>
$params
Tags
$queryString
The query string provided on the request (if any).
private
string
$queryString
Tags
$URI
The URI requested.
private
string
$URI
Tags
Methods
__construct()
Builds up the request based on available PHP super globals, in addition to any overrides provided (useful for testing).
public
__construct([array<string|int, mixed> $overrides = array() ]) : mixed
Parameters
- $overrides : array<string|int, mixed> = array()
-
Hash array of PHP super globals to override
Tags
addParams()
Append the hash array provided to the params for this request.
public
addParams(array<string|int, mixed> $params) : void
Parameters
- $params : array<string|int, mixed>
-
A hash array of values to add to the request params
Tags
getAccept()
Get the Accept header of the request.
public
getAccept() : string|null
Tags
Return values
string|nullgetBody()
Get the request body if one was provided.
public
getBody() : string
Tags
Return values
stringgetContentLength()
Get the Content-Length header of the request.
public
getContentLength() : string
Tags
Return values
stringgetContentType()
Get the Content-Type header of the request.
public
getContentType() : string|null
Tags
Return values
string|nullgetCookie()
Get the cookie matching the key provided.
public
getCookie(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
The key to search for
- $default : mixed = null
-
If key is not found, return this instead
Tags
getCookies()
Return all cookies on this request.
public
getCookies() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getFile()
Get the file matching the key provided.
public
getFile(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
The key to search for
- $default : mixed = null
-
If key is not found, return this instead
Tags
getFiles()
Return all files on this request.
public
getFiles() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getHeader()
Get the header matching the key provided.
public
getHeader(string $key[, mixed $default = null ]) : string|null
Parameters
- $key : string
-
The key to search for
- $default : mixed = null
-
If key is not found, return this instead
Tags
Return values
string|nullgetHeaders()
Return all headers on this request.
public
getHeaders() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getHost()
Get the host name of the client that sent the request.
public
getHost() : string
Tags
Return values
stringgetIP()
Get the IP address of the client that sent the request.
public
getIP() : string
Tags
Return values
stringgetMethod()
Get the HTTP method of this request.
public
getMethod() : string
Tags
Return values
stringgetParam()
Get the param matching the key provided.
public
getParam(string $key[, mixed $default = null ]) : string|null
Parameters
- $key : string
-
The key to search for
- $default : mixed = null
-
If key is not found, return this instead
Tags
Return values
string|nullgetParams()
Return all params on this request.
public
getParams() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getQueryString()
Get the query string provided on the request.
public
getQueryString() : string
Tags
Return values
stringgetReferrer()
Get the Referrer header of the request.
public
getReferrer() : string
Tags
Return values
stringgetURI()
Get the URI that was requested.
public
getURI() : string
Tags
Return values
stringgetURL()
Get the URL that was requested.
public
getURL() : string
Tags
Return values
stringgetUserAgent()
Get the User-Agent header of the request.
public
getUserAgent() : string|null
Tags
Return values
string|nullisSecureURI()
Checks to see if the request contains a secure/encrypted token.
public
isSecureURI() : bool
Tags
Return values
boolparseParamsFromRoute()
Parses the route provided to extract matching params of the route from this request's URI.
public
parseParamsFromRoute(string $route[, array<string|int, mixed> $defaultParams = array() ]) : void
Parameters
- $route : string
-
The route with parameter names, e.g. /user/{username}
- $defaultParams : array<string|int, mixed> = array()
-
Optional hash array of default request param values to use if they are missing from URI
Tags
setMethod()
Set the HTTP method of this request.
public
setMethod(string $method) : void
Parameters
- $method : string
Tags
setParams()
Set the params array.
public
setParams(array<string|int, mixed> $params) : void
Parameters
- $params : array<string|int, mixed>
-
A hash array of values to set as the request params
Tags
getGlobalBody()
Attempts to get the raw body of the current request from super globals.
private
getGlobalBody() : string
Tags
Return values
stringgetGlobalHeaders()
Tries to get the current HTTP request headers from super globals.
private
getGlobalHeaders() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getGlobalServerValue()
Tries to get the requested param from the $_SERVER super global, otherwise returns an empty string.
private
getGlobalServerValue(string $param) : string
Parameters
- $param : string