CodeIgniter v4.5 API

CURLRequest extends OutgoingRequest
in package

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

Tags
see
CURLRequestTest

Table of Contents

Properties

$baseURI  : URI
The URI associated with this request
$body  : string|null
Message body
$config  : array<string|int, mixed>
The setting values
$defaultConfig  : array<string|int, mixed>
The default setting values
$delay  : float
The number of milliseconds to delay before sending the request.
$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|array<int, Header>>
List of all HTTP request headers.
$method  : string
Request method.
$protocolVersion  : string
Protocol version
$redirectDefaults  : array<string|int, mixed>
Default values for when 'allow_redirects' option is true.
$response  : ResponseInterface|null
The response object associated with this request
$responseOrig  : ResponseInterface|null
The original response object associated with this request
$uri  : URI|null
A URI instance.
$validProtocolVersions  : array<string|int, mixed>
List of valid protocol versions
$defaultOptions  : array<string|int, mixed>
The default options from the constructor. Applied to all requests.
$shareOptions  : bool
Whether share options between requests or not.

Methods

__construct()  : mixed
Takes an array of options to set the following possible class properties:
addHeader()  : $this
Adds a header (not a header value) with the same name.
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.
get()  : ResponseInterface
Convenience method for sending a GET request.
getBody()  : string|null
Returns the Message's body.
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.
getMethod()  : string
Retrieves the HTTP method of the request.
getProtocolVersion()  : string
Returns the HTTP Protocol Version.
getUri()  : URI|null
Retrieves the URI instance.
hasHeader()  : bool
Determines whether a header exists.
head()  : ResponseInterface
Convenience method for sending a HEAD request.
header()  : Header|array<int, 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|array<int, Header>>
Returns an array containing all Headers.
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 server 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.
setHeader()  : $this
Sets a header and it's value.
setJSON()  : $this
Set JSON data to be sent.
setMethod()  : $this
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.
withUri()  : static
Returns an instance with the provided URI.
applyBody()  : array<string|int, mixed>
Apply body
applyMethod()  : array<string|int, mixed>
Apply method
applyRequestHeaders()  : array<string|int, mixed>
Adds $this->headers to the cURL request.
getHeaderName()  : string
Takes a header name in any case, and returns the normal-case version of the header.
parseOptions()  : void
Sets the correct settings based on the options array passed in.
prepareURL()  : string
If the $url is a relative URL, will attempt to create a full URL by prepending $this->baseURI to it.
resetOptions()  : void
Reset all options to default.
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()  : void
Parses the header retrieved from the cURL response into our Response object.
checkMultipleHeaders()  : void
getHostFromUri()  : string
hasMultipleHeaders()  : bool
isHostHeaderMissingOrEmpty()  : bool

Properties

$baseURI

The URI associated with this request

protected URI $baseURI

$body

Message body

protected string|null $body

$config

The setting values

protected array<string|int, mixed> $config

$defaultConfig

The default setting values

protected array<string|int, mixed> $defaultConfig = ['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

$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|array<int, Header>> $headers = []

[name => Header] or [name => [Header1, Header2]]

$protocolVersion

Protocol version

protected string $protocolVersion

$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', '3.0']

$defaultOptions read-only

The default options from the constructor. Applied to all requests.

private array<string|int, mixed> $defaultOptions

$shareOptions read-only

Whether share options between requests or not.

private bool $shareOptions

If true, all the options won't be reset between requests. It may cause an error request with unnecessary headers.

Methods

__construct()

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

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

addHeader()

Adds a header (not a header value) with the same name.

public addHeader(string $name, string $value) : $this

Use this only when you set multiple headers with the same name, typically, for Set-Cookie.

Parameters
$name : string
$value : string
Return values
$this

appendBody()

Appends data to the body of the current message.

public appendBody(string $data) : $this
Parameters
$data : string
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

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() : string|null
Return values
string|null

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

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

Parameters
$name : string
Tags
TODO

Incompatible return value with 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>

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

Tags
TODO

Incompatible return value with PSR-7

codeCoverageIgnore
Return values
array<string, Header>

An array of the request headers

getMethod()

Retrieves the HTTP method of the request.

public getMethod() : string
Return values
string

Returns the request method (always uppercase)

getProtocolVersion()

Returns the HTTP Protocol Version.

public getProtocolVersion() : string
Return values
string

HTTP protocol version.

hasHeader()

Determines whether a header exists.

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

Case-insensitive header field name.

Return values
bool

Returns true if any header names match the given header name using a case-insensitive string comparison. Returns false if no matching header name is found in the message.

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) : Header|array<int, Header>|null
Parameters
$name : string
Return values
Header|array<int, Header>|null

headers()

Returns an array containing all Headers.

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

An array of the Header objects

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 server knows about.

public populateHeaders() : 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

HTTP method

$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(string $data) : $this
Parameters
$data : string
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

setHeader()

Sets a header and it's value.

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

setJSON()

Set JSON data to be sent.

public setJSON(array<string|int, mixed>|bool|float|int|object|string|null $data) : $this
Parameters
$data : array<string|int, mixed>|bool|float|int|object|string|null
Return values
$this

setMethod()

Sets the request method. Used when spoofing the request.

public setMethod(string $method) : $this

Use withMethod() instead for immutability

Parameters
$method : string
Return values
$this

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

withUri()

Returns an instance with the provided URI.

public withUri(URI $uri[, bool $preserveHost = false ]) : static
Parameters
$uri : URI

New request URI to use.

$preserveHost : bool = false

Preserve the original state of the Host header.

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()

Adds $this->headers 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) : void
Parameters
$options : array<string|int, 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

resetOptions()

Reset all options to default.

protected resetOptions() : void

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 = [] ]) : void
Parameters
$headers : array<string|int, mixed> = []

checkMultipleHeaders()

private checkMultipleHeaders(string $name) : void
Parameters
$name : string

getHostFromUri()

private getHostFromUri(URI $uri) : string
Parameters
$uri : URI
Return values
string

hasMultipleHeaders()

private hasMultipleHeaders(string $name) : bool
Parameters
$name : string
Return values
bool

isHostHeaderMissingOrEmpty()

private isHostHeaderMissingOrEmpty() : bool
Return values
bool

        
On this page

Search results