Request
in package
Get information about the incoming HTTP request.
Table of Contents
Properties
- $accept_types : mixed
- $http_methods : mixed
Methods
- accepts() : bool
- Returns boolean of whether client accepts content type.
- acceptsAtQuality() : int|float
- Returns quality factor at which the client accepts content type.
- getAuthorization() : null|string
- Get the authorization header for this request.
- getBody() : array<string|int, mixed>|DOMDocument|string|null
- Sanitise and decode the body based on the request 'content-type'.
- getBodyParam() : mixed
- Get a single body parameter.
- getContentType() : null|string
- Get the content type for this request, if any.
- getForwarded() : ($type is string ? string : array{for?: string, proto?: string, host?: string, by?: string})|null
- Parse the RFC 7239 Forwarded header.
- getHeader() : string|null
- Get a single header value.
- getHeaders() : array<string, string>
- Get the headers as an array.
- getHeaderValues() : array<string|int, string>|null
- Get all values of a single header.
- getParam() : string|array<string|int, mixed>|null
- Get a single parameter, either from the query or body.
- getParams() : array<string|int, mixed>
- getQueryParam() : string|array<string|int, mixed>|null
- Get a single query parameter.
- getQueryParams() : array<string|int, mixed>
- Get all query parameters.
- getQueryString() : string
- The search query.
- getRawBody() : string|array<string|int, mixed>|null
- Get the raw body of the request.
- getTag() : string
- Get the request tag.
- getTime() : float
- Get the request time in unix, with microseconds.
- isAjax() : bool
- Tests if the current request is an AJAX request by checking the X-Requested-With HTTP request header that most popular JS frameworks now set for AJAX calls.
- isIframe() : bool
- Tests if the current request is within an iframe.
- isLoadBalanced() : bool
- Is this request coming from a load balancer?
- method() : string
- Returns current request method.
- preferredAccept() : mixed
- Compare the q values for given array of content types and return the one with the highest value.
- protocol() : string|null
- Returns the current request protocol, based on $_SERVER['https']. In CLI mode, NULL will be returned.
- referrer() : string
- Returns the HTTP referrer, or the default if the referrer is not set.
- userIp() : mixed
- Returns the users IP address, accounting for proxy forwarding
- parseAcceptHeader() : void
- Parses client's HTTP Accept request header, and builds array structure representing it.
Properties
$accept_types
protected
static mixed
$accept_types
$http_methods
protected
static mixed
$http_methods
= array('get', 'head', 'options', 'post', 'put', 'patch', 'delete')
Methods
accepts()
Returns boolean of whether client accepts content type.
public
static accepts([string|null $type = null ][, bool $explicit_check = false ]) : bool
Parameters
- $type : string|null = null
-
Content type
- $explicit_check : bool = false
-
Set to TRUE to disable wildcard checking
Return values
boolacceptsAtQuality()
Returns quality factor at which the client accepts content type.
public
static acceptsAtQuality([string|null $type = null ][, bool $explicit_check = false ]) : int|float
Parameters
- $type : string|null = null
-
Content type (e.g. "image/jpg", "jpg")
- $explicit_check : bool = false
-
Set to TRUE to disable wildcard checking
Return values
int|floatgetAuthorization()
Get the authorization header for this request.
public
static getAuthorization(string $type) : null|string
Given a header like 'Bearer abc123', this will return 'abc123'.
Parameters
- $type : string
-
typically 'bearer' or 'basic'
Return values
null|stringgetBody()
Sanitise and decode the body based on the request 'content-type'.
public
static getBody(string|array<string|int, string> ...$types) : array<string|int, mixed>|DOMDocument|string|null
Supported types:
multipart/form-data=> arrayapplication/x-www-form-urlencoded=> arrayapplication/json=> arraytext/xml=> DOMDocument
Optionally specify types to limit the 'permitted' types. This will return null if the 'content-type' header doesn't match at least one.
Parameters
- $types : string|array<string|int, string>
-
Expected/valid types
Tags
Return values
array<string|int, mixed>|DOMDocument|string|nullgetBodyParam()
Get a single body parameter.
public
static getBodyParam(string $name) : mixed
Parameters
- $name : string
Tags
getContentType()
Get the content type for this request, if any.
public
static getContentType() : null|string
Return values
null|stringgetForwarded()
Parse the RFC 7239 Forwarded header.
public
static getForwarded([string|null $type = null ]) : ($type is string ? string : array{for?: string, proto?: string, host?: string, by?: string})|null
Parameters
- $type : string|null = null
-
one of: for, proto, host, by
Return values
($type is string ? string : array{for?: string, proto?: string, host?: string, by?: string})|nullgetHeader()
Get a single header value.
public
static getHeader(string $name) : string|null
The value will include all values as the original string.
e.g. application/json; charset=utf-8
Parameters
- $name : string
Return values
string|nullgetHeaders()
Get the headers as an array.
public
static getHeaders() : array<string, string>
All keys are lowercase.
Return values
array<string, string> —[ name => value ]
getHeaderValues()
Get all values of a single header.
public
static getHeaderValues(string $name[, bool $associated = true ]) : array<string|int, string>|null
Parameters
- $name : string
- $associated : bool = true
Return values
array<string|int, string>|nullgetParam()
Get a single parameter, either from the query or body.
public
static getParam(string $name) : string|array<string|int, mixed>|null
Query params take precedence.
Parameters
- $name : string
Tags
Return values
string|array<string|int, mixed>|nullgetParams()
public
static getParams() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>getQueryParam()
Get a single query parameter.
public
static getQueryParam(string $name) : string|array<string|int, mixed>|null
Parameters
- $name : string
Return values
string|array<string|int, mixed>|nullgetQueryParams()
Get all query parameters.
public
static getQueryParams() : array<string|int, mixed>
This is the same as $_GET.
Return values
array<string|int, mixed>getQueryString()
The search query.
public
static getQueryString() : string
Should be the same as Router::$query_string.
Return values
stringgetRawBody()
Get the raw body of the request.
public
static getRawBody() : string|array<string|int, mixed>|null
Beware, this is not sanitised or decoded.
Return values
string|array<string|int, mixed>|nullgetTag()
Get the request tag.
public
static getTag() : string
Known as SPROUT_REQUEST_TAG.
Also X-Sprout-Tag (when not in production).
Return values
stringgetTime()
Get the request time in unix, with microseconds.
public
static getTime() : float
Return values
floatisAjax()
Tests if the current request is an AJAX request by checking the X-Requested-With HTTP request header that most popular JS frameworks now set for AJAX calls.
public
static isAjax() : bool
Return values
boolisIframe()
Tests if the current request is within an iframe.
public
static isIframe() : bool
Return values
boolisLoadBalanced()
Is this request coming from a load balancer?
public
static isLoadBalanced() : bool
Whether to trust forwarded headers for userIp() and protocol().
The sprout.load_balanced config can be:
- array: a list of IP addresses or CIDR ranges to trust
- true: always trust forwarded headers
- false: never trust forwarded headers
Return values
boolmethod()
Returns current request method.
public
static method() : string
Tags
Return values
stringpreferredAccept()
Compare the q values for given array of content types and return the one with the highest value.
public
static preferredAccept(array<string|int, mixed> $types[, bool $explicit_check = false ]) : mixed
If items are found to have the same q value, the first one encountered in the given array wins. If all items in the given array have a q value of 0, FALSE is returned.
Parameters
- $types : array<string|int, mixed>
-
Content types
- $explicit_check : bool = false
-
Set to TRUE to disable wildcard checking
Return values
mixed —string mime type with highest q value, FALSE if none of the given types are accepted
protocol()
Returns the current request protocol, based on $_SERVER['https']. In CLI mode, NULL will be returned.
public
static protocol() : string|null
Return values
string|nullreferrer()
Returns the HTTP referrer, or the default if the referrer is not set.
public
static referrer([mixed $default = false ]) : string
Parameters
- $default : mixed = false
-
Default to return
Return values
stringuserIp()
Returns the users IP address, accounting for proxy forwarding
public
static userIp() : mixed
parseAcceptHeader()
Parses client's HTTP Accept request header, and builds array structure representing it.
protected
static parseAcceptHeader() : void