Request
extends Message
in package
implements
RequestInterface
uses
RequestTrait
Implements methods for HTTP requests.
Used by Cake\Http\Client to contain request information for making requests.
Table of Contents
Interfaces
- RequestInterface
- Representation of an outgoing, client-side request.
Constants
- METHOD_DELETE = 'DELETE'
- HTTP DELETE method
- METHOD_GET = 'GET'
- HTTP GET method
- METHOD_HEAD = 'HEAD'
- HTTP HEAD method
- METHOD_OPTIONS = 'OPTIONS'
- HTTP OPTIONS method
- METHOD_PATCH = 'PATCH'
- HTTP PATCH method
- METHOD_POST = 'POST'
- HTTP POST method
- METHOD_PUT = 'PUT'
- HTTP PUT method
- METHOD_TRACE = 'TRACE'
- HTTP TRACE method
- STATUS_ACCEPTED = 202
- HTTP 202 code
- STATUS_CREATED = 201
- HTTP 201 code
- STATUS_FOUND = 302
- HTTP 302 code
- STATUS_MOVED_PERMANENTLY = 301
- HTTP 301 code
- STATUS_NO_CONTENT = 204
- HTTP 204 code
- STATUS_NON_AUTHORITATIVE_INFORMATION = 203
- HTTP 203 code
- STATUS_OK = 200
- HTTP 200 code
- STATUS_PERMANENT_REDIRECT = 308
- HTTP 308 code
- STATUS_SEE_OTHER = 303
- HTTP 303 code
- STATUS_TEMPORARY_REDIRECT = 307
- HTTP 307 code
Properties
- $_cookies : array<string|int, mixed>
- The array of cookies in the response.
- $method : string
- $requestTarget : null|string
- The request-target, if it has been provided or calculated.
- $uri : UriInterface
Methods
- __construct() : mixed
- Constructor
- cookies() : array<string|int, mixed>
- Get all cookies
- getMethod() : string
- Retrieves the HTTP method of the request.
- getRequestTarget() : string
- Retrieves the message's request target.
- getUri() : UriInterface
- Retrieves the URI instance.
- withMethod() : static
- Return an instance with the provided HTTP method.
- withRequestTarget() : static
- Create a new instance with a specific request-target.
- withUri() : static
- Returns an instance with the provided URI.
- addHeaders() : void
- Add an array of headers to the request.
- setContent() : $this
- Set the body/payload for the message.
- createUri() : UriInterface
- Create and return a URI instance.
- getHostFromUri() : string
- Retrieve the host from the URI instance
- initialize() : void
- Initialize request state.
- setMethod() : void
- Set and validate the HTTP method
Constants
METHOD_DELETE
HTTP DELETE method
public
string
METHOD_DELETE
= 'DELETE'
METHOD_GET
HTTP GET method
public
string
METHOD_GET
= 'GET'
METHOD_HEAD
HTTP HEAD method
public
string
METHOD_HEAD
= 'HEAD'
METHOD_OPTIONS
HTTP OPTIONS method
public
string
METHOD_OPTIONS
= 'OPTIONS'
METHOD_PATCH
HTTP PATCH method
public
string
METHOD_PATCH
= 'PATCH'
METHOD_POST
HTTP POST method
public
string
METHOD_POST
= 'POST'
METHOD_PUT
HTTP PUT method
public
string
METHOD_PUT
= 'PUT'
METHOD_TRACE
HTTP TRACE method
public
string
METHOD_TRACE
= 'TRACE'
STATUS_ACCEPTED
HTTP 202 code
public
int
STATUS_ACCEPTED
= 202
STATUS_CREATED
HTTP 201 code
public
int
STATUS_CREATED
= 201
STATUS_FOUND
HTTP 302 code
public
int
STATUS_FOUND
= 302
STATUS_MOVED_PERMANENTLY
HTTP 301 code
public
int
STATUS_MOVED_PERMANENTLY
= 301
STATUS_NO_CONTENT
HTTP 204 code
public
int
STATUS_NO_CONTENT
= 204
STATUS_NON_AUTHORITATIVE_INFORMATION
HTTP 203 code
public
int
STATUS_NON_AUTHORITATIVE_INFORMATION
= 203
STATUS_OK
HTTP 200 code
public
int
STATUS_OK
= 200
STATUS_PERMANENT_REDIRECT
HTTP 308 code
public
int
STATUS_PERMANENT_REDIRECT
= 308
STATUS_SEE_OTHER
HTTP 303 code
public
int
STATUS_SEE_OTHER
= 303
STATUS_TEMPORARY_REDIRECT
HTTP 307 code
public
int
STATUS_TEMPORARY_REDIRECT
= 307
Properties
$_cookies
The array of cookies in the response.
protected
array<string|int, mixed>
$_cookies
= []
$method
private
string
$method
= 'GET'
$requestTarget
The request-target, if it has been provided or calculated.
private
null|string
$requestTarget
$uri
private
UriInterface
$uri
Methods
__construct()
Constructor
public
__construct([string $url = '' ][, string $method = self::METHOD_GET ][, array<string|int, mixed> $headers = [] ][, array<string|int, mixed>|string|null $data = null ]) : mixed
Provides backwards compatible defaults for some properties.
Parameters
- $url : string = ''
-
The request URL
- $method : string = self::METHOD_GET
-
The HTTP method to use.
- $headers : array<string|int, mixed> = []
-
The HTTP headers to set.
- $data : array<string|int, mixed>|string|null = null
-
The request body to use.
Tags
cookies()
Get all cookies
public
cookies() : array<string|int, mixed>
Return values
array<string|int, mixed>getMethod()
Retrieves the HTTP method of the request.
public
getMethod() : string
Return values
string —Returns the request method.
getRequestTarget()
Retrieves the message's request target.
public
getRequestTarget() : string
Retrieves the message's request-target either as it will appear (for clients), as it appeared at request (for servers), or as it was specified for the instance (see withRequestTarget()).
In most cases, this will be the origin-form of the composed URI, unless a value was provided to the concrete implementation (see withRequestTarget() below).
If no URI is available, and no request-target has been specifically provided, this method MUST return the string "/".
Return values
stringgetUri()
Retrieves the URI instance.
public
getUri() : UriInterface
This method MUST return a UriInterface instance.
Tags
Return values
UriInterface —Returns a UriInterface instance representing the URI of the request, if any.
withMethod()
Return an instance with the provided HTTP method.
public
withMethod(string $method) : static
While HTTP method names are typically all uppercase characters, HTTP method names are case-sensitive and thus implementations SHOULD NOT modify the given string.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the changed request method.
Parameters
- $method : string
-
Case-insensitive method.
Tags
Return values
staticwithRequestTarget()
Create a new instance with a specific request-target.
public
withRequestTarget(string $requestTarget) : static
If the request needs a non-origin-form request-target — e.g., for specifying an absolute-form, authority-form, or asterisk-form — this method may be used to create an instance with the specified request-target, verbatim.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return a new instance that has the changed request target.
Parameters
- $requestTarget : string
Tags
Return values
staticwithUri()
Returns an instance with the provided URI.
public
withUri(UriInterface $uri[, bool $preserveHost = false ]) : static
This method will update the Host header of the returned request by default if the URI contains a host component. If the URI does not contain a host component, any pre-existing Host header will be carried over to the returned request.
You can opt-in to preserving the original state of the Host header by
setting $preserveHost
to true
. When $preserveHost
is set to
true
, the returned request will not update the Host header of the
returned message -- even if the message contains no Host header. This
means that a call to getHeader('Host')
on the original request MUST
equal the return value of a call to getHeader('Host')
on the returned
request.
This method MUST be implemented in such a way as to retain the immutability of the message, and MUST return an instance that has the new UriInterface instance.
Parameters
- $uri : UriInterface
-
New request URI to use.
- $preserveHost : bool = false
-
Preserve the original state of the Host header.
Tags
Return values
staticaddHeaders()
Add an array of headers to the request.
protected
addHeaders(array<string, string> $headers) : void
Parameters
- $headers : array<string, string>
-
The headers to add.
Tags
setContent()
Set the body/payload for the message.
protected
setContent(array<string|int, mixed>|string $content) : $this
Array data will be serialized with , and the content-type will be set.
Parameters
- $content : array<string|int, mixed>|string
-
The body for the request.
Return values
$thiscreateUri()
Create and return a URI instance.
private
createUri(null|string|UriInterface $uri) : UriInterface
If $uri
is a already a UriInterface
instance, returns it.
If $uri
is a string, passes it to the Uri
constructor to return an
instance.
If $uri is null, creates and returns an empty
Uri` instance.
Otherwise, it raises an exception.
Parameters
- $uri : null|string|UriInterface
Tags
Return values
UriInterfacegetHostFromUri()
Retrieve the host from the URI instance
private
getHostFromUri() : string
Return values
stringinitialize()
Initialize request state.
private
initialize([null|string|UriInterface $uri = null ][, null|string $method = null ][, string|resource|StreamInterface $body = 'php://memory' ][, array<string|int, mixed> $headers = [] ]) : void
Used by constructors.
Parameters
- $uri : null|string|UriInterface = null
-
URI for the request, if any.
- $method : null|string = null
-
HTTP method for the request, if any.
- $body : string|resource|StreamInterface = 'php://memory'
-
Message body, if any.
- $headers : array<string|int, mixed> = []
-
Headers for the message, if any.
Tags
setMethod()
Set and validate the HTTP method
private
setMethod(string $method) : void
Parameters
- $method : string