Request
extends Message
in package
implements
RequestInterface
The Request class represents a single HTTP request.
You can either simply construct the object from scratch, or if you need access to the current HTTP request, use Sapi::getRequest.
Tags
Interfaces, Classes and Traits
- RequestInterface
- The RequestInterface represents a HTTP request.
Table of Contents
- $absoluteUrl : mixed
- $baseUrl : string
- Base url.
- $body : resource|string|callable
- Request body.
- $headers : array<string|int, mixed>
- Contains the list of HTTP headers.
- $httpVersion : string
- HTTP message version (1.0, 1.1 or 2.0).
- $method : string
- HTTP Method.
- $postData : array<string|int, mixed>
- Equivalent of PHP's $_POST.
- $rawServerData : array<string|int, mixed>
- An array containing the raw _SERVER array.
- $url : string
- Request Url.
- __construct() : mixed
- Creates the request object.
- __toString() : string
- Serializes the request object as a string.
- addHeader() : mixed
- Adds a HTTP header.
- addHeaders() : mixed
- Adds a new set of HTTP headers.
- getAbsoluteUrl() : string
- Returns the absolute url.
- getBaseUrl() : string
- Returns the current base url.
- getBody() : resource|string|callable
- Returns the message body, as it's internal representation.
- getBodyAsStream() : resource
- Returns the body as a readable stream resource.
- getBodyAsString() : string
- Returns the body as a string.
- getHeader() : string|null
- Returns a specific HTTP header, based on it's name.
- getHeaderAsArray() : array<string|int, string>
- Returns a HTTP header as an array.
- getHeaders() : array<string|int, mixed>
- Returns all the HTTP headers as an array.
- getHttpVersion() : string
- Returns the HTTP version.
- getMethod() : string
- Returns the current HTTP method.
- getPath() : string
- Returns the relative path.
- getPostData() : array<string|int, mixed>
- Returns the POST data.
- getQueryParameters() : array<string|int, mixed>
- Returns the list of query parameters.
- getRawServerValue() : string|null
- Returns an item from the _SERVER array.
- getUrl() : string
- Returns the request url.
- hasHeader() : bool
- Will return true or false, depending on if a HTTP header exists.
- removeHeader() : bool
- Removes a HTTP header.
- setAbsoluteUrl() : mixed
- Sets the absolute url.
- setBaseUrl() : mixed
- Sets a base url.
- setBody() : mixed
- Replaces the body resource with a new stream, string or a callback writing the body to php://output.
- setHeader() : mixed
- Updates a HTTP header.
- setHeaders() : mixed
- Sets a new set of HTTP headers.
- setHttpVersion() : mixed
- Sets the HTTP version.
- setMethod() : mixed
- Sets the HTTP method.
- setPostData() : mixed
- Sets the post data.
- setRawServerData() : mixed
- Sets the _SERVER array.
- setUrl() : mixed
- Sets the request url.
Properties
$absoluteUrl
protected
mixed
$absoluteUrl
$baseUrl
Base url.
protected
string
$baseUrl
= '/'
$body
Request body.
protected
resource|string|callable
$body
This should be a stream resource, string or a callback writing the body to php://output
$headers
Contains the list of HTTP headers.
protected
array<string|int, mixed>
$headers
= []
$httpVersion
HTTP message version (1.0, 1.1 or 2.0).
protected
string
$httpVersion
= '1.1'
$method
HTTP Method.
protected
string
$method
$postData
Equivalent of PHP's $_POST.
protected
array<string|int, mixed>
$postData
= []
$rawServerData
An array containing the raw _SERVER array.
protected
array<string|int, mixed>
$rawServerData
$url
Request Url.
protected
string
$url
Methods
__construct()
Creates the request object.
public
__construct(string $method, string $url[, array<string|int, mixed> $headers = [] ][, resource|callable|string $body = null ]) : mixed
Parameters
- $method : string
- $url : string
- $headers : array<string|int, mixed> = []
- $body : resource|callable|string = null
Return values
mixed —__toString()
Serializes the request object as a string.
public
__toString() : string
This is useful for debugging purposes.
Return values
string —addHeader()
Adds a HTTP header.
public
addHeader(string $name, string|array<string|int, string> $value) : mixed
This method will not overwrite any existing HTTP header, but instead add another value. Individual values can be retrieved with getHeadersAsArray.
Parameters
- $name : string
- $value : string|array<string|int, string>
Return values
mixed —addHeaders()
Adds a new set of HTTP headers.
public
addHeaders(array<string|int, mixed> $headers) : mixed
Any existing headers will not be overwritten.
Parameters
- $headers : array<string|int, mixed>
Return values
mixed —getAbsoluteUrl()
Returns the absolute url.
public
getAbsoluteUrl() : string
Return values
string —getBaseUrl()
Returns the current base url.
public
getBaseUrl() : string
Return values
string —getBody()
Returns the message body, as it's internal representation.
public
getBody() : resource|string|callable
This could be either a string, a stream or a callback writing the body to php://output.
Return values
resource|string|callable —getBodyAsStream()
Returns the body as a readable stream resource.
public
getBodyAsStream() : resource
Note that the stream may not be rewindable, and therefore may only be read once.
Return values
resource —getBodyAsString()
Returns the body as a string.
public
getBodyAsString() : string
Note that because the underlying data may be based on a stream, this method could only work correctly the first time.
Return values
string —getHeader()
Returns a specific HTTP header, based on it's name.
public
getHeader(string $name) : string|null
The name must be treated as case-insensitive. If the header does not exist, this method must return null.
If a header appeared more than once in a HTTP request, this method will concatenate all the values with a comma.
Note that this not make sense for all headers. Some, such as
Set-Cookie cannot be logically combined with a comma. In those cases
you should use getHeaderAsArray().
Parameters
- $name : string
Return values
string|null —getHeaderAsArray()
Returns a HTTP header as an array.
public
getHeaderAsArray(string $name) : array<string|int, string>
For every time the HTTP header appeared in the request or response, an item will appear in the array.
If the header did not exists, this method will return an empty array.
Parameters
- $name : string
Return values
array<string|int, string> —getHeaders()
Returns all the HTTP headers as an array.
public
getHeaders() : array<string|int, mixed>
Every header is returned as an array, with one or more values.
Return values
array<string|int, mixed> —getHttpVersion()
Returns the HTTP version.
public
getHttpVersion() : string
Return values
string —getMethod()
Returns the current HTTP method.
public
getMethod() : string
Return values
string —getPath()
Returns the relative path.
public
getPath() : string
This is being calculated using the base url. This path will not start with a slash, so it will always return something like 'example/path.html'.
If the full path is equal to the base url, this method will return an empty string.
This method will also urldecode the path, and if the url was incoded as ISO-8859-1, it will convert it to UTF-8.
If the path is outside of the base url, a LogicException will be thrown.
Return values
string —getPostData()
Returns the POST data.
public
getPostData() : array<string|int, mixed>
This is equivalent to PHP's $_POST superglobal.
Return values
array<string|int, mixed> —getQueryParameters()
Returns the list of query parameters.
public
getQueryParameters() : array<string|int, mixed>
This is equivalent to PHP's $_GET superglobal.
Return values
array<string|int, mixed> —getRawServerValue()
Returns an item from the _SERVER array.
public
getRawServerValue(string $valueName) : string|null
If the value does not exist in the array, null is returned.
Parameters
- $valueName : string
Return values
string|null —getUrl()
Returns the request url.
public
getUrl() : string
Return values
string —hasHeader()
Will return true or false, depending on if a HTTP header exists.
public
hasHeader(string $name) : bool
Parameters
- $name : string
Return values
bool —removeHeader()
Removes a HTTP header.
public
removeHeader(string $name) : bool
The specified header name must be treated as case-insensitive. This method should return true if the header was successfully deleted, and false if the header did not exist.
Parameters
- $name : string
Return values
bool —setAbsoluteUrl()
Sets the absolute url.
public
setAbsoluteUrl(string $url) : mixed
Parameters
- $url : string
Return values
mixed —setBaseUrl()
Sets a base url.
public
setBaseUrl(string $url) : mixed
This url is used for relative path calculations.
Parameters
- $url : string
Return values
mixed —setBody()
Replaces the body resource with a new stream, string or a callback writing the body to php://output.
public
setBody(resource|string|callable $body) : mixed
Parameters
- $body : resource|string|callable
Return values
mixed —setHeader()
Updates a HTTP header.
public
setHeader(string $name, string|array<string|int, string> $value) : mixed
The case-sensitivity of the name value must be retained as-is.
If the header already existed, it will be overwritten.
Parameters
- $name : string
- $value : string|array<string|int, string>
Return values
mixed —setHeaders()
Sets a new set of HTTP headers.
public
setHeaders(array<string|int, mixed> $headers) : mixed
The headers array should contain headernames for keys, and their value should be specified as either a string or an array.
Any header that already existed will be overwritten.
Parameters
- $headers : array<string|int, mixed>
Return values
mixed —setHttpVersion()
Sets the HTTP version.
public
setHttpVersion(string $version) : mixed
Should be 1.0, 1.1 or 2.0.
Parameters
- $version : string
Return values
mixed —setMethod()
Sets the HTTP method.
public
setMethod(string $method) : mixed
Parameters
- $method : string
Return values
mixed —setPostData()
Sets the post data.
public
setPostData(array<string|int, mixed> $postData) : mixed
This is equivalent to PHP's $_POST superglobal.
This would not have been needed, if POST data was accessible as php://input, but unfortunately we need to special case it.
Parameters
- $postData : array<string|int, mixed>
Return values
mixed —setRawServerData()
Sets the _SERVER array.
public
setRawServerData(array<string|int, mixed> $data) : mixed
Parameters
- $data : array<string|int, mixed>
Return values
mixed —setUrl()
Sets the request url.
public
setUrl(string $url) : mixed
Parameters
- $url : string