Documentation

CURLRequest extends Request
in package

Class OutgoingRequest

A lightweight HTTP client for sending synchronous HTTP requests via cURL.

Table of Contents

$baseURI  : URI
The URI associated with this request
$body  : mixed
Message body
$config  : array<string|int, mixed>
The setting values
$delay  : float
The number of milliseconds to delay before sending the request.
$globals  : array<string|int, mixed>
Stores values we've retrieved from PHP globals.
$headerMap  : array<string|int, mixed>
Holds a map of lower-case header names and their normal-case key as it is in $headers.
$headers  : array<string, Header>
List of all HTTP request headers.
$ipAddress  : string
IP address of the current user.
$method  : string
Request method.
$protocolVersion  : string
Protocol version
$proxyIPs  : string|array<string|int, mixed>
Proxy IPs
$redirectDefaults  : array<string|int, mixed>
Default values for when 'allow_redirects' option is true.
$response  : ResponseInterface|null
The response object associated with this request
$uri  : URI
A URI instance.
$validProtocolVersions  : array<string|int, mixed>
List of valid protocol versions
__construct()  : mixed
Takes an array of options to set the following possible class properties:
appendBody()  : $this
Appends data to the body of the current message.
appendHeader()  : $this
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
delete()  : ResponseInterface
Convenience method for sending a DELETE request.
fetchGlobal()  : mixed
Fetches one or more items from a global, like cookies, get, post, etc.
get()  : ResponseInterface
Convenience method for sending a GET request.
getBody()  : mixed
Returns the Message's body.
getEnv()  : mixed
Fetch an item from the $_ENV array.
getHeader()  : array<string|int, mixed>|Header|null
Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.
getHeaderLine()  : string
Retrieves a comma-separated string of the values for a single header.
getHeaders()  : array<string, Header>
Returns an array containing all headers.
getIPAddress()  : string
Gets the user's IP address.
getMethod()  : string
Get the request method. Overrides the Request class' method since users expect a different answer here.
getProtocolVersion()  : string
Returns the HTTP Protocol Version.
getServer()  : mixed
Fetch an item from the $_SERVER array.
getUri()  : URI
Retrieves the URI instance.
hasHeader()  : bool
Determines whether a header exists.
head()  : ResponseInterface
Convenience method for sending a HEAD request.
header()  : array<string|int, mixed>|Header|null
Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.
headers()  : array<string, Header>
Returns an array containing all Headers.
isValidIP()  : bool
Validate an IP address
options()  : ResponseInterface
Convenience method for sending an OPTIONS request.
patch()  : ResponseInterface
Convenience method for sending a PATCH request.
populateHeaders()  : void
Populates the $headers array with any headers the getServer knows about.
post()  : ResponseInterface
Convenience method for sending a POST request.
prependHeader()  : $this
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
put()  : ResponseInterface
Convenience method for sending a PUT request.
removeHeader()  : $this
Removes a header from the list of headers we track.
request()  : ResponseInterface
Sends an HTTP request to the specified $url. If this is a relative URL, it will be merged with $this->baseURI to form a complete URL.
send()  : ResponseInterface
Fires the actual cURL request.
setAuth()  : $this
Set the HTTP Authentication.
setBody()  : $this
Sets the body of the current message.
setForm()  : $this
Set form data to be sent.
setGlobal()  : $this
Allows manually setting the value of PHP global, like $_GET, $_POST, etc.
setHeader()  : $this
Sets a header and it's value.
setJSON()  : $this
Set JSON data to be sent.
setMethod()  : Request
Sets the request method. Used when spoofing the request.
setProtocolVersion()  : $this
Sets the HTTP protocol version.
withMethod()  : static
Returns an instance with the specified method.
applyBody()  : array<string|int, mixed>
Apply body
applyMethod()  : array<string|int, mixed>
Apply method
applyRequestHeaders()  : array<string|int, mixed>
Takes all headers current part of this request and adds them to the cURL request.
getHeaderName()  : string
Takes a header name in any case, and returns the normal-case version of the header.
parseOptions()  : mixed
Sets the correct settings based on the options array passed in.
populateGlobals()  : mixed
Saves a copy of the current state of one of several PHP globals so we can retrieve them later.
prepareURL()  : string
If the $url is a relative URL, will attempt to create a full URL by prepending $this->baseURI to it.
sendRequest()  : string
Does the actual work of initializing cURL, setting the options, and grabbing the output.
setCURLOptions()  : array<string|int, mixed>
Set CURL options
setResponseHeaders()  : mixed
Parses the header retrieved from the cURL response into our Response object.

Properties

$baseURI

The URI associated with this request

protected URI $baseURI

$body

Message body

protected mixed $body

$config

The setting values

protected array<string|int, mixed> $config = ['timeout' => 0.0, 'connect_timeout' => 150, 'debug' => false, 'verify' => true]

$delay

The number of milliseconds to delay before sending the request.

protected float $delay = 0.0

$globals

Stores values we've retrieved from PHP globals.

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

$headerMap

Holds a map of lower-case header names and their normal-case key as it is in $headers.

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

Used for case-insensitive header access.

$headers

List of all HTTP request headers.

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

$ipAddress

IP address of the current user.

protected string $ipAddress = ''
Tags
deprecated

Will become private in a future release

$method

Request method.

protected string $method

$protocolVersion

Protocol version

protected string $protocolVersion

$proxyIPs

Proxy IPs

protected string|array<string|int, mixed> $proxyIPs
Tags
deprecated

Check the App config directly

$redirectDefaults

Default values for when 'allow_redirects' option is true.

protected array<string|int, mixed> $redirectDefaults = ['max' => 5, 'strict' => true, 'protocols' => ['http', 'https']]

$validProtocolVersions

List of valid protocol versions

protected array<string|int, mixed> $validProtocolVersions = ['1.0', '1.1', '2.0']

Methods

__construct()

Takes an array of options to set the following possible class properties:

public __construct(App $config, URI $uri[, ResponseInterface $response = null ][, array<string|int, mixed> $options = [] ]) : mixed
  • baseURI
  • timeout
  • any other request options to use as defaults.
Parameters
$config : App
$uri : URI
$response : ResponseInterface = null
$options : array<string|int, mixed> = []
Return values
mixed

appendBody()

Appends data to the body of the current message.

public appendBody(mixed $data) : $this
Parameters
$data : mixed
Return values
$this

appendHeader()

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

public appendHeader(string $name, string|null $value) : $this
Parameters
$name : string
$value : string|null
Return values
$this

delete()

Convenience method for sending a DELETE request.

public delete(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

fetchGlobal()

Fetches one or more items from a global, like cookies, get, post, etc.

public fetchGlobal(string $method[, string|array<string|int, mixed>|null $index = null ][, int|null $filter = null ][, array<string|int, mixed>|int|null $flags = null ]) : mixed

Can optionally filter the input when you retrieve it by passing in a filter.

If $type is an array, it must conform to the input allowed by the filter_input_array method.

http://php.net/manual/en/filter.filters.sanitize.php

Parameters
$method : string

Input filter constant

$index : string|array<string|int, mixed>|null = null
$filter : int|null = null

Filter constant

$flags : array<string|int, mixed>|int|null = null

Options

Return values
mixed

get()

Convenience method for sending a GET request.

public get(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

getBody()

Returns the Message's body.

public getBody() : mixed
Return values
mixed

getEnv()

Fetch an item from the $_ENV array.

public getEnv([null $index = null ][, null $filter = null ][, null $flags = null ]) : mixed
Parameters
$index : null = null

Index for item to be fetched from $_ENV

$filter : null = null

A filter name to be applied

$flags : null = null
Return values
mixed

getHeader()

Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.

public getHeader(string $name) : array<string|int, mixed>|Header|null
Parameters
$name : string
Tags
deprecated

Use Message::header() to make room for PSR-7

codeCoverageIgnore
Return values
array<string|int, mixed>|Header|null

getHeaderLine()

Retrieves a comma-separated string of the values for a single header.

public getHeaderLine(string $name) : string

This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.

NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.

Parameters
$name : string
Return values
string

getHeaders()

Returns an array containing all headers.

public getHeaders() : array<string, Header>
Tags
deprecated

Use Message::headers() to make room for PSR-7

codeCoverageIgnore
Return values
array<string, Header>

An array of the request headers

getIPAddress()

Gets the user's IP address.

public getIPAddress() : string
Return values
string

IP address

getMethod()

Get the request method. Overrides the Request class' method since users expect a different answer here.

public getMethod([bool|false $upper = false ]) : string
Parameters
$upper : bool|false = false

Whether to return in upper or lower case.

Return values
string

getProtocolVersion()

Returns the HTTP Protocol Version.

public getProtocolVersion() : string
Return values
string

getServer()

Fetch an item from the $_SERVER array.

public getServer([string|array<string|int, mixed>|null $index = null ][, int|null $filter = null ][, null $flags = null ]) : mixed
Parameters
$index : string|array<string|int, mixed>|null = null

Index for item to be fetched from $_SERVER

$filter : int|null = null

A filter name to be applied

$flags : null = null
Return values
mixed

getUri()

Retrieves the URI instance.

public getUri() : URI
Return values
URI

hasHeader()

Determines whether a header exists.

public hasHeader(string $name) : bool
Parameters
$name : string
Return values
bool

head()

Convenience method for sending a HEAD request.

public head(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

header()

Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.

public header(string $name) : array<string|int, mixed>|Header|null
Parameters
$name : string
Return values
array<string|int, mixed>|Header|null

headers()

Returns an array containing all Headers.

public headers() : array<string, Header>
Return values
array<string, Header>

An array of the Header objects

isValidIP()

Validate an IP address

public isValidIP([string $ip = null ][, string $which = null ]) : bool
Parameters
$ip : string = null

IP Address

$which : string = null

IP protocol: 'ipv4' or 'ipv6'

Tags
deprecated

Use Validation instead

codeCoverageIgnore
Return values
bool

options()

Convenience method for sending an OPTIONS request.

public options(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

patch()

Convenience method for sending a PATCH request.

public patch(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

populateHeaders()

Populates the $headers array with any headers the getServer knows about.

public populateHeaders() : void
Return values
void

post()

Convenience method for sending a POST request.

public post(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

prependHeader()

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

public prependHeader(string $name, string $value) : $this
Parameters
$name : string
$value : string
Return values
$this

put()

Convenience method for sending a PUT request.

public put(string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

removeHeader()

Removes a header from the list of headers we track.

public removeHeader(string $name) : $this
Parameters
$name : string
Return values
$this

request()

Sends an HTTP request to the specified $url. If this is a relative URL, it will be merged with $this->baseURI to form a complete URL.

public request(string $method, string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
$method : string
$url : string
$options : array<string|int, mixed> = []
Return values
ResponseInterface

setAuth()

Set the HTTP Authentication.

public setAuth(string $username, string $password[, string $type = 'basic' ]) : $this
Parameters
$username : string
$password : string
$type : string = 'basic'

basic or digest

Return values
$this

setBody()

Sets the body of the current message.

public setBody(mixed $data) : $this
Parameters
$data : mixed
Return values
$this

setForm()

Set form data to be sent.

public setForm(array<string|int, mixed> $params[, bool $multipart = false ]) : $this
Parameters
$params : array<string|int, mixed>
$multipart : bool = false

Set TRUE if you are sending CURLFiles

Return values
$this

setGlobal()

Allows manually setting the value of PHP global, like $_GET, $_POST, etc.

public setGlobal(string $method, mixed $value) : $this
Parameters
$method : string
$value : mixed
Return values
$this

setHeader()

Sets a header and it's value.

public setHeader(string $name, array<string|int, mixed>|null|string $value) : $this
Parameters
$name : string
$value : array<string|int, mixed>|null|string
Return values
$this

setJSON()

Set JSON data to be sent.

public setJSON(mixed $data) : $this
Parameters
$data : mixed
Return values
$this

setMethod()

Sets the request method. Used when spoofing the request.

public setMethod(string $method) : Request
Parameters
$method : string
Tags
deprecated

Use withMethod() instead for immutability

codeCoverageIgnore
Return values
Request

setProtocolVersion()

Sets the HTTP protocol version.

public setProtocolVersion(string $version) : $this
Parameters
$version : string
Tags
throws
HTTPException

For invalid protocols

Return values
$this

withMethod()

Returns an instance with the specified method.

public withMethod(string $method) : static
Parameters
$method : string
Return values
static

applyBody()

Apply body

protected applyBody([array<string|int, mixed> $curlOptions = [] ]) : array<string|int, mixed>
Parameters
$curlOptions : array<string|int, mixed> = []
Return values
array<string|int, mixed>

applyMethod()

Apply method

protected applyMethod(string $method, array<string|int, mixed> $curlOptions) : array<string|int, mixed>
Parameters
$method : string
$curlOptions : array<string|int, mixed>
Return values
array<string|int, mixed>

applyRequestHeaders()

Takes all headers current part of this request and adds them to the cURL request.

protected applyRequestHeaders([array<string|int, mixed> $curlOptions = [] ]) : array<string|int, mixed>
Parameters
$curlOptions : array<string|int, mixed> = []
Return values
array<string|int, mixed>

getHeaderName()

Takes a header name in any case, and returns the normal-case version of the header.

protected getHeaderName(string $name) : string
Parameters
$name : string
Return values
string

parseOptions()

Sets the correct settings based on the options array passed in.

protected parseOptions(array<string|int, mixed> $options) : mixed
Parameters
$options : array<string|int, mixed>
Return values
mixed

populateGlobals()

Saves a copy of the current state of one of several PHP globals so we can retrieve them later.

protected populateGlobals(string $method) : mixed
Parameters
$method : string
Return values
mixed

prepareURL()

If the $url is a relative URL, will attempt to create a full URL by prepending $this->baseURI to it.

protected prepareURL(string $url) : string
Parameters
$url : string
Return values
string

sendRequest()

Does the actual work of initializing cURL, setting the options, and grabbing the output.

protected sendRequest([array<string|int, mixed> $curlOptions = [] ]) : string
Parameters
$curlOptions : array<string|int, mixed> = []
Tags
codeCoverageIgnore
Return values
string

setCURLOptions()

Set CURL options

protected setCURLOptions([array<string|int, mixed> $curlOptions = [] ][, array<string|int, mixed> $config = [] ]) : array<string|int, mixed>
Parameters
$curlOptions : array<string|int, mixed> = []
$config : array<string|int, mixed> = []
Tags
throws
InvalidArgumentException
Return values
array<string|int, mixed>

setResponseHeaders()

Parses the header retrieved from the cURL response into our Response object.

protected setResponseHeaders([array<string|int, mixed> $headers = [] ]) : mixed
Parameters
$headers : array<string|int, mixed> = []
Return values
mixed

Search results