CurlHttpClient
in package
implements
HttpClientInterface, LoggerAwareInterface, ResetInterface
uses
HttpClientTrait
A performant implementation of the HttpClientInterface contracts based on the curl extension.
This provides fully concurrent HTTP requests, with transparent HTTP/2 push when a curl version that supports it is installed.
Tags
Table of Contents
Interfaces
- HttpClientInterface
- Provides flexible methods for requesting HTTP resources synchronously or asynchronously.
- LoggerAwareInterface
- Describes a logger-aware instance.
- ResetInterface
- Provides a way to reset an object to its initial state.
Properties
- $CHUNK_SIZE : mixed
- $defaultOptions : mixed
- $emptyDefaults : mixed
- $logger : LoggerInterface|null
- $maxHostConnections : mixed
- $maxPendingPushes : mixed
- $multi : CurlClientState
- An internal object to share state between the client and its responses.
Methods
- __construct() : mixed
- request() : ResponseInterface
- Requests an HTTP resource.
- reset() : mixed
- setLogger() : void
- Sets a logger instance on the object.
- stream() : ResponseStreamInterface
- Yields responses chunk by chunk as they complete.
- withOptions() : self
- {@inheritdoc}
- acceptPushForRequest() : bool
- Accepts pushed responses only if their headers related to authentication match the request.
- createRedirectResolver() : Closure
- Resolves relative URLs on redirects and deals with authentication headers.
- dechunk() : string
- ensureState() : CurlClientState
- findConstantName() : string|null
- getProxy() : array<string|int, mixed>|null
- Loads proxy configuration from the same environment variables as curl when no proxy is explicitly set.
- getProxyUrl() : string|null
- jsonEncode() : string
- mergeDefaultOptions() : array<string|int, mixed>
- mergeQueryString() : string|null
- Merges and encodes a query array with a query string.
- normalizeBody() : string|resource|Closure
- normalizeHeaders() : array<string|int, array<string|int, string>>
- normalizePeerFingerprint() : array<string|int, mixed>
- parseUrl() : array<string|int, mixed>
- Parses a URL and fixes its encoding if needed.
- prepareRequest() : array<string|int, mixed>
- Validates and normalizes method, URL and options, and merges them with defaults.
- readRequestBody() : string
- Wraps the request's body callback to allow it to return strings longer than curl requested.
- removeDotSegments() : mixed
- Removes dot-segments from a path.
- resolveUrl() : array<string|int, mixed>
- Resolves a URL against a base URI.
- shouldBuffer() : bool
- validateExtraCurlOptions() : void
- Prevents overriding options that are set internally throughout the request.
Properties
$CHUNK_SIZE
private
static mixed
$CHUNK_SIZE
= 16372
$defaultOptions
private
mixed
$defaultOptions
= self::OPTIONS_DEFAULTS + [
'auth_ntlm' => null,
// array|string - an array containing the username as first value, and optionally the
// password as the second one; or string like username:password - enabling NTLM auth
'extra' => ['curl' => []],
]
$emptyDefaults
private
static mixed
$emptyDefaults
= self::OPTIONS_DEFAULTS + ['auth_ntlm' => null]
$logger
private
LoggerInterface|null
$logger
$maxHostConnections
private
mixed
$maxHostConnections
$maxPendingPushes
private
mixed
$maxPendingPushes
$multi
An internal object to share state between the client and its responses.
private
CurlClientState
$multi
Methods
__construct()
public
__construct([array<string|int, mixed> $defaultOptions = [] ][, int $maxHostConnections = 6 ][, int $maxPendingPushes = 50 ]) : mixed
Parameters
- $defaultOptions : array<string|int, mixed> = []
-
Default request's options
- $maxHostConnections : int = 6
-
The maximum number of connections to a single host
- $maxPendingPushes : int = 50
-
The maximum number of pushed responses to accept in the queue
Tags
request()
Requests an HTTP resource.
public
request(string $method, string $url[, array<string|int, mixed> $options = [] ]) : ResponseInterface
Parameters
- $method : string
- $url : string
- $options : array<string|int, mixed> = []
Tags
Return values
ResponseInterfacereset()
public
reset() : mixed
setLogger()
Sets a logger instance on the object.
public
setLogger(LoggerInterface $logger) : void
Parameters
- $logger : LoggerInterface
stream()
Yields responses chunk by chunk as they complete.
public
stream(mixed $responses[, float|null $timeout = null ]) : ResponseStreamInterface
Parameters
- $responses : mixed
-
One or more responses created by the current HTTP client
- $timeout : float|null = null
-
The idle timeout before yielding timeout chunks
Return values
ResponseStreamInterfacewithOptions()
{@inheritdoc}
public
withOptions(array<string|int, mixed> $options) : self
Parameters
- $options : array<string|int, mixed>
Return values
selfacceptPushForRequest()
Accepts pushed responses only if their headers related to authentication match the request.
private
static acceptPushForRequest(string $method, array<string|int, mixed> $options, PushedResponse $pushedResponse) : bool
Parameters
- $method : string
- $options : array<string|int, mixed>
- $pushedResponse : PushedResponse
Return values
boolcreateRedirectResolver()
Resolves relative URLs on redirects and deals with authentication headers.
private
static createRedirectResolver(array<string|int, mixed> $options, string $host) : Closure
Work around CVE-2018-1000007: Authorization and Cookie headers should not follow redirects - fixed in Curl 7.64
Parameters
- $options : array<string|int, mixed>
- $host : string
Return values
Closuredechunk()
private
static dechunk(string $body) : string
Parameters
- $body : string
Return values
stringensureState()
private
ensureState() : CurlClientState
Return values
CurlClientStatefindConstantName()
private
findConstantName(int $opt) : string|null
Parameters
- $opt : int
Return values
string|nullgetProxy()
Loads proxy configuration from the same environment variables as curl when no proxy is explicitly set.
private
static getProxy(string|null $proxy, array<string|int, mixed> $url, string|null $noProxy) : array<string|int, mixed>|null
Parameters
- $proxy : string|null
- $url : array<string|int, mixed>
- $noProxy : string|null
Return values
array<string|int, mixed>|nullgetProxyUrl()
private
static getProxyUrl(string|null $proxy, array<string|int, mixed> $url) : string|null
Parameters
- $proxy : string|null
- $url : array<string|int, mixed>
Return values
string|nulljsonEncode()
private
static jsonEncode(mixed $value[, int|null $flags = null ][, int $maxDepth = 512 ]) : string
Parameters
- $value : mixed
- $flags : int|null = null
- $maxDepth : int = 512
Tags
Return values
stringmergeDefaultOptions()
private
static mergeDefaultOptions(array<string|int, mixed> $options, array<string|int, mixed> $defaultOptions[, bool $allowExtraOptions = false ]) : array<string|int, mixed>
Parameters
- $options : array<string|int, mixed>
- $defaultOptions : array<string|int, mixed>
- $allowExtraOptions : bool = false
Tags
Return values
array<string|int, mixed>mergeQueryString()
Merges and encodes a query array with a query string.
private
static mergeQueryString(string|null $queryString, array<string|int, mixed> $queryArray, bool $replace) : string|null
Parameters
- $queryString : string|null
- $queryArray : array<string|int, mixed>
- $replace : bool
Tags
Return values
string|nullnormalizeBody()
private
static normalizeBody(array<string|int, mixed>|string|resource|Traversable|Closure $body) : string|resource|Closure
Parameters
- $body : array<string|int, mixed>|string|resource|Traversable|Closure
Tags
Return values
string|resource|ClosurenormalizeHeaders()
private
static normalizeHeaders(array<string|int, mixed> $headers) : array<string|int, array<string|int, string>>
Parameters
- $headers : array<string|int, mixed>
Tags
Return values
array<string|int, array<string|int, string>>normalizePeerFingerprint()
private
static normalizePeerFingerprint(string|array<string|int, string> $fingerprint) : array<string|int, mixed>
Parameters
- $fingerprint : string|array<string|int, string>
Tags
Return values
array<string|int, mixed>parseUrl()
Parses a URL and fixes its encoding if needed.
private
static parseUrl(string $url[, array<string|int, mixed> $query = [] ][, array<string|int, mixed> $allowedSchemes = ['http' => 80, 'https' => 443] ]) : array<string|int, mixed>
Parameters
- $url : string
- $query : array<string|int, mixed> = []
- $allowedSchemes : array<string|int, mixed> = ['http' => 80, 'https' => 443]
Tags
Return values
array<string|int, mixed>prepareRequest()
Validates and normalizes method, URL and options, and merges them with defaults.
private
static prepareRequest(string|null $method, string|null $url, array<string|int, mixed> $options[, array<string|int, mixed> $defaultOptions = [] ][, bool $allowExtraOptions = false ]) : array<string|int, mixed>
Parameters
- $method : string|null
- $url : string|null
- $options : array<string|int, mixed>
- $defaultOptions : array<string|int, mixed> = []
- $allowExtraOptions : bool = false
Tags
Return values
array<string|int, mixed>readRequestBody()
Wraps the request's body callback to allow it to return strings longer than curl requested.
private
static readRequestBody(int $length, Closure $body, string &$buffer, bool &$eof) : string
Parameters
- $length : int
- $body : Closure
- $buffer : string
- $eof : bool
Return values
stringremoveDotSegments()
Removes dot-segments from a path.
private
static removeDotSegments(string $path) : mixed
Parameters
- $path : string
Tags
resolveUrl()
Resolves a URL against a base URI.
private
static resolveUrl(array<string|int, mixed> $url, array<string|int, mixed>|null $base[, array<string|int, mixed> $queryDefaults = [] ]) : array<string|int, mixed>
Parameters
- $url : array<string|int, mixed>
- $base : array<string|int, mixed>|null
- $queryDefaults : array<string|int, mixed> = []
Tags
Return values
array<string|int, mixed>shouldBuffer()
private
static shouldBuffer(array<string|int, mixed> $headers) : bool
Parameters
- $headers : array<string|int, mixed>
Return values
boolvalidateExtraCurlOptions()
Prevents overriding options that are set internally throughout the request.
private
validateExtraCurlOptions(array<string|int, mixed> $options) : void
Parameters
- $options : array<string|int, mixed>