Alpha Framework 4.0.0 API Documentation

Request
in package

A class to encapsulate a HTTP request.

Tags
since
2.0
author

John Collins dev@alphaframework.org

license

http://www.opensource.org/licenses/bsd-license.php The BSD License

copyright

Copyright (c) 2021, John Collins (founder of Alpha Framework). All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.
* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.
* Neither the name of the Alpha Framework nor the names
  of its contributors may be used to endorse or promote
  products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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
since
2.0

$cookies

An associative array of HTTP cookies on this request.

private array<string|int, mixed> $cookies
Tags
since
2.0

$files

An associative 3D array of uploaded files.

private array<string|int, mixed> $files
Tags
since
2.0

$headers

An associative array of HTTP headers on this request.

private array<string|int, mixed> $headers
Tags
since
2.0

$host

The host header provided on the request.

private string $host
Tags
since
2.0

$HTTPMethods

Array of supported HTTP methods.

private array<string|int, mixed> $HTTPMethods = array('HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE')
Tags
since
2.0

$IP

The IP of the client making the request.

private string $IP
Tags
since
2.0

$method

The HTTP method of this request (must be in HTTPMethods array).

private string $method
Tags
since
2.0

$params

The HTTP params (form data and query string) on this request.

private array<string|int, mixed> $params
Tags
since
2.0

$queryString

The query string provided on the request (if any).

private string $queryString
Tags
since
2.0

$URI

The URI requested.

private string $URI
Tags
since
2.0

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
throws
IllegalArguementException
since
2.0

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
since
2.0

getAccept()

Get the Accept header of the request.

public getAccept() : string|null
Tags
since
2.0
Return values
string|null

getBody()

Get the request body if one was provided.

public getBody() : string
Tags
since
2.0
Return values
string

getContentLength()

Get the Content-Length header of the request.

public getContentLength() : string
Tags
since
2.0
Return values
string

getContentType()

Get the Content-Type header of the request.

public getContentType() : string|null
Tags
since
2.0
Return values
string|null

getCookie()

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
since
2.0

getCookies()

Return all cookies on this request.

public getCookies() : array<string|int, mixed>
Tags
since
2.0
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
since
2.0

getFiles()

Return all files on this request.

public getFiles() : array<string|int, mixed>
Tags
since
2.0
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
since
2.0
Return values
string|null

getHeaders()

Return all headers on this request.

public getHeaders() : array<string|int, mixed>
Tags
since
2.0
Return values
array<string|int, mixed>

getHost()

Get the host name of the client that sent the request.

public getHost() : string
Tags
since
2.0
Return values
string

getIP()

Get the IP address of the client that sent the request.

public getIP() : string
Tags
since
2.0
Return values
string

getMethod()

Get the HTTP method of this request.

public getMethod() : string
Tags
since
2.0
Return values
string

getParam()

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
since
2.0
Return values
string|null

getParams()

Return all params on this request.

public getParams() : array<string|int, mixed>
Tags
since
2.0
Return values
array<string|int, mixed>

getQueryString()

Get the query string provided on the request.

public getQueryString() : string
Tags
since
2.0
Return values
string

getReferrer()

Get the Referrer header of the request.

public getReferrer() : string
Tags
since
2.0
Return values
string

getURI()

Get the URI that was requested.

public getURI() : string
Tags
since
2.0
Return values
string

getURL()

Get the URL that was requested.

public getURL() : string
Tags
since
2.0
Return values
string

getUserAgent()

Get the User-Agent header of the request.

public getUserAgent() : string|null
Tags
since
2.0
Return values
string|null

isSecureURI()

Checks to see if the request contains a secure/encrypted token.

public isSecureURI() : bool
Tags
since
2.0
Return values
bool

parseParamsFromRoute()

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
since
2.0

setMethod()

Set the HTTP method of this request.

public setMethod(string $method) : void
Parameters
$method : string
Tags
throws
IllegalArguementException
since
2.0

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
since
2.0

getGlobalBody()

Attempts to get the raw body of the current request from super globals.

private getGlobalBody() : string
Tags
since
2.0
Return values
string

getGlobalHeaders()

Tries to get the current HTTP request headers from super globals.

private getGlobalHeaders() : array<string|int, mixed>
Tags
since
2.0
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
Tags
since
3.0
Return values
string

        
On this page

Search results