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
$url
HTTP Request URL.
protected
URL
$url
Methods
__construct()
Request constructor.
public
__construct(URL|string $url) : mixed
Parameters
- $url : URL|string
__toString()
public
__toString() : string
Attributes
- #[Override]
Return values
stringgetBody()
Get the Message body.
public
getBody() : string
Attributes
- #[Pure]
Return values
stringgetCookie()
Get a Cookie by name.
public
getCookie(string $name) : Cookie|null
Parameters
- $name : string
Attributes
- #[Pure]
Return values
Cookie|nullgetCookies()
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|nullgetHeaderLine()
public
getHeaderLine(string $name) : string|null
Parameters
- $name : string
Attributes
- #[Pure]
Return values
string|nullgetHeaderLines()
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
Return values
intgetMethod()
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
Return values
array<string, mixed>|stringgetProtocol()
Get the HTTP protocol.
public
getProtocol() : string
Attributes
- #[Pure]
Return values
stringgetStartLine()
Get the Message Start-Line.
public
getStartLine() : string
Tags
Return values
stringgetUrl()
Gets the requested URL.
public
getUrl() : URL
Attributes
- #[Override]
- #[Pure]
Return values
URLhasCookie()
Say if the Message has a Cookie.
public
hasCookie(string $name) : bool
Parameters
- $name : string
-
Cookie name
Attributes
- #[Pure]
Return values
boolhasFiles()
public
hasFiles() : bool
Attributes
- #[Pure]
Return values
boolhasHeader()
public
hasHeader(string $name[, string|null $value = null ]) : bool
Parameters
- $name : string
- $value : string|null = null
Attributes
- #[Pure]
Return values
boolisCheckingOptions()
public
isCheckingOptions() : bool
Return values
boolisGettingInfo()
public
isGettingInfo() : bool
Return values
boolisMethod()
public
isMethod(string $method) : bool
Parameters
- $method : string
Tags
Attributes
- #[Override]
Return values
boolparseQualityValues()
public
static parseQualityValues(string|null $string) : array<string, float>
Parameters
- $string : string|null
Tags
Return values
array<string, float>removeCookie()
Remove a Cookie by name.
public
removeCookie(string $name) : static
Parameters
- $name : string
Attributes
- #[Override]
Return values
staticremoveCookies()
Remove many Cookies by names.
public
removeCookies(array<int, string> $names) : static
Parameters
- $names : array<int, string>
Attributes
- #[Override]
Return values
staticremoveHeader()
Remove a header by name.
public
removeHeader(string $name) : static
Parameters
- $name : string
Attributes
- #[Override]
Return values
staticremoveHeaders()
Remove all headers.
public
removeHeaders() : static
Attributes
- #[Override]
Return values
staticsetBasicAuth()
Set Authorization header with Basic type.
public
setBasicAuth(string $username, string $password) : static
Parameters
- $username : string
- $password : string
Tags
Return values
staticsetBearerAuth()
Set Authorization header with Bearer type.
public
setBearerAuth(string $token) : static
Parameters
- $token : string
Return values
staticsetBody()
Set the request body.
public
setBody(array<string, mixed>|string $body) : static
Parameters
- $body : array<string, mixed>|string
Attributes
- #[Override]
Return values
staticsetCheckOptions()
public
setCheckOptions([bool $check = true ]) : static
Parameters
- $check : bool = true
Return values
staticsetContentType()
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
staticsetCookie()
Set a new Cookie.
public
setCookie(Cookie $cookie) : static
Parameters
- $cookie : Cookie
Attributes
- #[Override]
Return values
staticsetCookies()
Set a list of Cookies.
public
setCookies(array<int, Cookie> $cookies) : static
Parameters
- $cookies : array<int, Cookie>
Attributes
- #[Override]
Return values
staticsetDownloadFile()
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
Return values
staticsetDownloadFunction()
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
staticsetFiles()
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
Return values
staticsetGetInfo()
public
setGetInfo([bool $get = true ]) : static
Parameters
- $get : bool = true
Return values
staticsetHeader()
Set a Message header.
public
setHeader(string $name, string $value) : static
Parameters
- $name : string
- $value : string
Attributes
- #[Override]
Return values
staticsetHeaders()
Set a list of headers.
public
setHeaders(array<string, string> $headers) : static
Parameters
- $headers : array<string, string>
Attributes
- #[Override]
Return values
staticsetJson()
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
Return values
staticsetJsonFlags()
Set JSON flags.
public
setJsonFlags(int $flags) : static
Parameters
- $flags : int
Tags
Return values
staticsetMethod()
Set the request method.
public
setMethod(string $method) : static
Parameters
- $method : string
Attributes
- #[Override]
Return values
staticsetOption()
Set curl options.
public
setOption(int $option, mixed $value) : static
Parameters
- $option : int
-
A curl constant
- $value : mixed
Tags
Return values
staticsetOptions()
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
staticsetPost()
Set POST data simulating a browser request.
public
setPost(array<string, mixed> $data) : static
Parameters
- $data : array<string, mixed>
Return values
staticsetProtocol()
Set the HTTP protocol.
public
setProtocol(string $protocol) : static
Parameters
- $protocol : string
Attributes
- #[Override]
Return values
staticsetUrl()
Set the Message URL.
public
setUrl(URL|string $url) : static
Parameters
- $url : URL|string
Attributes
- #[Override]
Return values
staticsetUserAgent()
Set the User-Agent header.
public
setUserAgent([string|null $userAgent = null ]) : static
Parameters
- $userAgent : string|null = null
Tags
Return values
staticappendHeader()
Append a Message header.
protected
appendHeader(string $name, string $value) : static
Used to set repeated header field names.
Parameters
- $name : string
- $value : string
Tags
Return values
staticcheckOption()
protected
checkOption(int $option, mixed $value) : void
Parameters
- $option : int
-
The curl option
- $value : mixed
-
The curl option value
Tags
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
stringgetStatusCode()
Get the status code.
protected
getStatusCode() : int
Attributes
- #[Pure]
Return values
intisStatusCode()
protected
isStatusCode(int $code) : bool
Parameters
- $code : int
Return values
boolparseContentType()
protected
parseContentType() : string|null
Attributes
- #[Pure]
Return values
string|nullremoveHeadersByNames()
Remove headers by names.
protected
removeHeadersByNames(array<string|int, string> $names) : static
Parameters
- $names : array<string|int, string>
Return values
staticsetCookieHeader()
protected
setCookieHeader() : static
Return values
staticsetStatusCode()
protected
setStatusCode(int $code) : static
Parameters
- $code : int