Documentation

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
bool

acceptsAtQuality()

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|float

getAuthorization()

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|string

getBody()

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 => array
  • application/x-www-form-urlencoded => array
  • application/json => array
  • text/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
throws
JsonException
UrlDecodeException
XMLException
Return values
array<string|int, mixed>|DOMDocument|string|null

getContentType()

Get the content type for this request, if any.

public static getContentType() : null|string
Return values
null|string

getForwarded()

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})|null

getHeader()

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|null

getHeaders()

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>|null

getParam()

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
throws
JsonException
Return values
string|array<string|int, mixed>|null

getParams()

public static getParams() : array<string|int, mixed>
Tags
throws
JsonException
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>|null

getQueryParams()

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
string

getRawBody()

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>|null

getTag()

Get the request tag.

public static getTag() : string

Known as SPROUT_REQUEST_TAG.

Also X-Sprout-Tag (when not in production).

Return values
string

getTime()

Get the request time in unix, with microseconds.

public static getTime() : float
Return values
float

isAjax()

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
bool

isIframe()

Tests if the current request is within an iframe.

public static isIframe() : bool
Return values
bool

isLoadBalanced()

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
bool

method()

Returns current request method.

public static method() : string
Tags
throws
Kohana_Exception

in case of an unknown request method

Return values
string

preferredAccept()

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|null

referrer()

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
string

userIp()

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
On this page

Search results