Aplus Framework Docs

Request extends Message
in package
implements RequestInterface

Class Request.

Table of Contents

Interfaces

RequestInterface
Interface RequestInterface.

Properties

$body  : string
HTTP Message Body.
$checkOptions  : bool
$cookies  : array<string, Cookie>
HTTP Message Cookies.
$defaultOptions  : array<int, mixed>
Client default curl options.
$files  : array<string, array<string|int, mixed>|string>
POST files.
$getInfo  : bool
$headers  : array<string, string>
HTTP Message Headers.
$jsonFlags  : int
$method  : string
HTTP Request Method.
$options  : array<int, mixed>
Custom curl options.
$protocol  : string
HTTP Message Protocol.
$statusCode  : int
HTTP Response Status Code.
$url  : URL
HTTP Request URL.

Methods

__construct()  : mixed
Request constructor.
__toString()  : string
getBody()  : string
Get the Message body.
getCookie()  : Cookie|null
Get a Cookie by name.
getCookies()  : array<string, Cookie>
Get all Cookies.
getFiles()  : array<string|int, mixed>
Get files for upload.
getHeader()  : string|null
getHeaderLine()  : string|null
getHeaderLines()  : array<int, string>
getHeaders()  : array<string, string>
getJsonFlags()  : int
Get JSON flags.
getMethod()  : string
Gets the HTTP Request Method.
getOption()  : mixed
getOptions()  : array<int, mixed>
Get default options replaced by custom.
getPostAndFiles()  : array<string, mixed>|string
Returns string if the Request has not files and curl will set the Content-Type header to application/x-www-form-urlencoded. If the Request has files, returns an array and curl will set the Content-Type to multipart/form-data.
getProtocol()  : string
Get the HTTP protocol.
getStartLine()  : string
Get the Message Start-Line.
getUrl()  : URL
Gets the requested URL.
hasCookie()  : bool
Say if the Message has a Cookie.
hasFiles()  : bool
hasHeader()  : bool
isCheckingOptions()  : bool
isGettingInfo()  : bool
isMethod()  : bool
parseQualityValues()  : array<string, float>
removeCookie()  : static
Remove a Cookie by name.
removeCookies()  : static
Remove many Cookies by names.
removeHeader()  : static
Remove a header by name.
removeHeaders()  : static
Remove all headers.
setBasicAuth()  : static
Set Authorization header with Basic type.
setBearerAuth()  : static
Set Authorization header with Bearer type.
setBody()  : static
Set the request body.
setCheckOptions()  : static
setContentType()  : static
Set the Content-Type header.
setCookie()  : static
Set a new Cookie.
setCookies()  : static
Set a list of Cookies.
setDownloadFile()  : static
Set a filename to download the file.
setDownloadFunction()  : static
Set a callback to write the response body with chunks.
setFiles()  : static
Set files for upload.
setGetInfo()  : static
setHeader()  : static
Set a Message header.
setHeaders()  : static
Set a list of headers.
setJson()  : static
Set body with JSON data.
setJsonFlags()  : static
Set JSON flags.
setMethod()  : static
Set the request method.
setOption()  : static
Set curl options.
setOptions()  : static
Set many curl options.
setPost()  : static
Set POST data simulating a browser request.
setProtocol()  : static
Set the HTTP protocol.
setUrl()  : static
Set the Message URL.
setUserAgent()  : static
Set the User-Agent header.
appendHeader()  : static
Append a Message header.
checkOption()  : void
getFileInfo()  : array<string, string>
getMultipartBody()  : string
getStatusCode()  : int
Get the status code.
isStatusCode()  : bool
parseContentType()  : string|null
removeHeadersByNames()  : static
Remove headers by names.
setCookieHeader()  : static
setStatusCode()  : static

Properties

$body

HTTP Message Body.

protected string $body

$checkOptions

protected bool $checkOptions = false

$cookies

HTTP Message Cookies.

protected array<string, Cookie> $cookies = []

$defaultOptions

Client default curl options.

protected array<int, mixed> $defaultOptions = [\CURLOPT_CONNECTTIMEOUT => 10, \CURLOPT_TIMEOUT => 60, \CURLOPT_FOLLOWLOCATION => false, \CURLOPT_MAXREDIRS => 1, \CURLOPT_AUTOREFERER => true, \CURLOPT_RETURNTRANSFER => true, \CURLOPT_ENCODING => '']

$files

POST files.

protected array<string, array<string|int, mixed>|string> $files = []

$getInfo

protected bool $getInfo = false

$headers

HTTP Message Headers.

protected array<string, string> $headers = []

$jsonFlags

protected int $jsonFlags = \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE

$method

HTTP Request Method.

protected string $method = \Framework\HTTP\Method::GET

$options

Custom curl options.

protected array<int, mixed> $options = []

$protocol

HTTP Message Protocol.

protected string $protocol = \Framework\HTTP\Protocol::HTTP_1_1

$statusCode

HTTP Response Status Code.

protected int $statusCode

Methods

__construct()

Request constructor.

public __construct(URL|string $url) : mixed
Parameters
$url : URL|string

__toString()

public __toString() : string
Attributes
#[Override]
Return values
string

getBody()

Get the Message body.

public getBody() : string
Attributes
#[Pure]
Return values
string

getCookie()

Get a Cookie by name.

public getCookie(string $name) : Cookie|null
Parameters
$name : string
Attributes
#[Pure]
Return values
Cookie|null

getCookies()

Get all Cookies.

public getCookies() : array<string, Cookie>
Attributes
#[Pure]
Return values
array<string, Cookie>

getFiles()

Get files for upload.

public getFiles() : array<string|int, mixed>
Attributes
#[Pure]
Return values
array<string|int, mixed>

getHeader()

public getHeader(string $name) : string|null
Parameters
$name : string
Attributes
#[Pure]
Return values
string|null

getHeaderLine()

public getHeaderLine(string $name) : string|null
Parameters
$name : string
Attributes
#[Pure]
Return values
string|null

getHeaderLines()

public getHeaderLines() : array<int, string>
Attributes
#[Pure]
Return values
array<int, string>

getHeaders()

public getHeaders() : array<string, string>
Attributes
#[Pure]
Return values
array<string, string>

getJsonFlags()

Get JSON flags.

public getJsonFlags() : int
Tags
since
5.6
Return values
int

getMethod()

Gets the HTTP Request Method.

public getMethod() : string
Attributes
#[Override]
#[Pure]
Return values
string —

$method One of: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, or TRACE

getOption()

public getOption(int $option) : mixed
Parameters
$option : int

getOptions()

Get default options replaced by custom.

public getOptions() : array<int, mixed>
Return values
array<int, mixed>

getPostAndFiles()

Returns string if the Request has not files and curl will set the Content-Type header to application/x-www-form-urlencoded. If the Request has files, returns an array and curl will set the Content-Type to multipart/form-data.

public getPostAndFiles() : array<string, mixed>|string

If the Request has files, the $post and $files arrays are converted to the array_simple format. Because curl does not understand the PHP multi-dimensional arrays.

Tags
see
https://www.php.net/manual/en/function.curl-setopt.php

CURLOPT_POSTFIELDS

ArraySimple::convert()
Return values
array<string, mixed>|string

getProtocol()

Get the HTTP protocol.

public getProtocol() : string
Attributes
#[Pure]
Return values
string

getStartLine()

Get the Message Start-Line.

public getStartLine() : string
Tags
throws
BadMethodCallException

if $this is not an instance of RequestInterface or ResponseInterface

Return values
string

getUrl()

Gets the requested URL.

public getUrl() : URL
Attributes
#[Override]
#[Pure]
Return values
URL

hasCookie()

Say if the Message has a Cookie.

public hasCookie(string $name) : bool
Parameters
$name : string

Cookie name

Attributes
#[Pure]
Return values
bool

hasFiles()

public hasFiles() : bool
Attributes
#[Pure]
Return values
bool

hasHeader()

public hasHeader(string $name[, string|null $value = null ]) : bool
Parameters
$name : string
$value : string|null = null
Attributes
#[Pure]
Return values
bool

isCheckingOptions()

public isCheckingOptions() : bool
Return values
bool

isGettingInfo()

public isGettingInfo() : bool
Return values
bool

isMethod()

public isMethod(string $method) : bool
Parameters
$method : string
Tags
throws
InvalidArgumentException

for invalid method

Attributes
#[Override]
Return values
bool

removeCookie()

Remove a Cookie by name.

public removeCookie(string $name) : static
Parameters
$name : string
Attributes
#[Override]
Return values
static

removeCookies()

Remove many Cookies by names.

public removeCookies(array<int, string> $names) : static
Parameters
$names : array<int, string>
Attributes
#[Override]
Return values
static

removeHeader()

Remove a header by name.

public removeHeader(string $name) : static
Parameters
$name : string
Attributes
#[Override]
Return values
static

removeHeaders()

Remove all headers.

public removeHeaders() : static
Attributes
#[Override]
Return values
static

setBearerAuth()

Set Authorization header with Bearer type.

public setBearerAuth(string $token) : static
Parameters
$token : string
Return values
static

setBody()

Set the request body.

public setBody(array<string, mixed>|string $body) : static
Parameters
$body : array<string, mixed>|string
Attributes
#[Override]
Return values
static

setCheckOptions()

public setCheckOptions([bool $check = true ]) : static
Parameters
$check : bool = true
Return values
static

setContentType()

Set the Content-Type header.

public setContentType(string $mime[, string|null $charset = 'UTF-8' ]) : static
Parameters
$mime : string
$charset : string|null = 'UTF-8'
Return values
static

setCookie()

Set a new Cookie.

public setCookie(Cookie $cookie) : static
Parameters
$cookie : Cookie
Attributes
#[Override]
Return values
static

setCookies()

Set a list of Cookies.

public setCookies(array<int, Cookie> $cookies) : static
Parameters
$cookies : array<int, Cookie>
Attributes
#[Override]
Return values
static

setDownloadFile()

Set a filename to download the file.

public setDownloadFile(string $filename[, bool $overwrite = false ]) : static
Parameters
$filename : string

The filename

$overwrite : bool = false

Set true to allow to overwrite the file

Tags
throws
RuntimeException

if $overwrite is false and the file exists

Return values
static

setDownloadFunction()

Set a callback to write the response body with chunks.

public setDownloadFunction(callable $callback) : static

Used to write data to files, databases, etc...

NOTE: Using this function makes the Response body, returned in the Client::run() method, be set with an empty string.

Parameters
$callback : callable

A callback with the response body $data chunk as first argument and the CurlHandle as the second. Return is not necessary.

Return values
static

setFiles()

Set files for upload.

public setFiles(array<string|int, mixed> $files) : static
Parameters
$files : array<string|int, mixed>

Fields as keys, files (CURLFile, CURLStringFile or string filename) as values. Multi-dimensional array is allowed.

Tags
throws
InvalidArgumentException

for invalid file path

Return values
static

setGetInfo()

public setGetInfo([bool $get = true ]) : static
Parameters
$get : bool = true
Return values
static

setHeader()

Set a Message header.

public setHeader(string $name, string $value) : static
Parameters
$name : string
$value : string
Attributes
#[Override]
Return values
static

setHeaders()

Set a list of headers.

public setHeaders(array<string, string> $headers) : static
Parameters
$headers : array<string, string>
Attributes
#[Override]
Return values
static

setJson()

Set body with JSON data.

public setJson(mixed $data[, int|null $flags = null ][, int<1, max> $depth = 512 ]) : static
Parameters
$data : mixed
$flags : int|null = null

[optional]

Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_AMP, JSON_HEX_APOS, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_THROW_ON_ERROR, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE.

Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE when null. JSON_THROW_ON_ERROR is enforced by default.

$depth : int<1, max> = 512

[optional] Set the maximum depth. Must be greater than zero.

Tags
see
https://www.php.net/manual/en/function.json-encode.php
https://www.php.net/manual/en/json.constants.php
throws
JsonException

if json_encode() fails

Return values
static

setJsonFlags()

Set JSON flags.

public setJsonFlags(int $flags) : static
Parameters
$flags : int
Tags
since
5.6
Return values
static

setMethod()

Set the request method.

public setMethod(string $method) : static
Parameters
$method : string
Attributes
#[Override]
Return values
static

setOption()

Set curl options.

public setOption(int $option, mixed $value) : static
Parameters
$option : int

A curl constant

$value : mixed
Tags
see
Client::$defaultOptions
Return values
static

setOptions()

Set many curl options.

public setOptions(array<int, mixed> $options) : static
Parameters
$options : array<int, mixed>

Curl constants as keys and their respective values

Return values
static

setPost()

Set POST data simulating a browser request.

public setPost(array<string, mixed> $data) : static
Parameters
$data : array<string, mixed>
Return values
static

setProtocol()

Set the HTTP protocol.

public setProtocol(string $protocol) : static
Parameters
$protocol : string
Attributes
#[Override]
Return values
static

setUrl()

Set the Message URL.

public setUrl(URL|string $url) : static
Parameters
$url : URL|string
Attributes
#[Override]
Return values
static

checkOption()

protected checkOption(int $option, mixed $value) : void
Parameters
$option : int

The curl option

$value : mixed

The curl option value

Tags
throws
InvalidArgumentException

if the option value does not match the expected type

OutOfBoundsException

if the option is invalid

getFileInfo()

protected getFileInfo(CURLFile|CURLStringFile|string $file) : array<string, string>
Parameters
$file : CURLFile|CURLStringFile|string
Attributes
#[ArrayShape]
['filename' => 'string', 'data' => 'string', 'mime' => 'string']
Return values
array<string, string>

getMultipartBody()

protected getMultipartBody() : string
Return values
string

getStatusCode()

Get the status code.

protected getStatusCode() : int
Attributes
#[Pure]
Return values
int

isStatusCode()

protected isStatusCode(int $code) : bool
Parameters
$code : int
Return values
bool

parseContentType()

protected parseContentType() : string|null
Attributes
#[Pure]
Return values
string|null

removeHeadersByNames()

Remove headers by names.

protected removeHeadersByNames(array<string|int, string> $names) : static
Parameters
$names : array<string|int, string>
Return values
static

setCookieHeader()

protected setCookieHeader() : static
Return values
static

setStatusCode()

protected setStatusCode(int $code) : static
Parameters
$code : int
Return values
static

        
On this page

Search results