NativeHttpClient
in package
implements
HttpClientInterface, LoggerAwareInterface
Uses
HttpClientTrait, LoggerAwareTrait
A portable implementation of the HttpClientInterface contracts based on PHP stream wrappers.
PHP stream wrappers are able to fetch response bodies concurrently, but each request is opened synchronously.
Tags
Interfaces, Classes, Traits and Enums
- HttpClientInterface
- Provides flexible methods for requesting HTTP resources synchronously or asynchronously.
- LoggerAwareInterface
- Describes a logger-aware instance.
Table of Contents
- $logger : LoggerInterface|null
- The logger instance.
- $CHUNK_SIZE : mixed
- $defaultOptions : mixed
- $multi : NativeClientState
- __construct() : mixed
- request() : ResponseInterface
- Requests an HTTP resource.
- setLogger() : mixed
- Sets a logger.
- stream() : ResponseStreamInterface
- Yields responses chunk by chunk as they complete.
- configureHeadersAndProxy() : bool
- createRedirectResolver() : Closure
- Handles redirects - the native logic is too buggy to be used.
- dnsResolve() : string
- Resolves the IP of the host using the local DNS cache if possible.
- getBodyAsString() : string
- getProxy() : array<string|int, mixed>|null
- Loads proxy configuration from the same environment variables as curl when no proxy is explicitly set.
- 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>
- parseHostPort() : array<string|int, mixed>
- Extracts the host and the port from the URL.
- 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.
- removeDotSegments() : mixed
- Removes dot-segments from a path.
- resolveUrl() : array<string|int, mixed>
- Resolves a URL against a base URI.
- shouldBuffer() : bool
Properties
$logger
The logger instance.
protected
LoggerInterface|null
$logger
$CHUNK_SIZE
private
static mixed
$CHUNK_SIZE
= 16372
$defaultOptions
private
mixed
$defaultOptions
= self::OPTIONS_DEFAULTS
$multi
private
NativeClientState
$multi
Methods
__construct()
public
__construct([array<string|int, mixed> $defaultOptions = [] ][, int $maxHostConnections = 6 ]) : mixed
Parameters
- $defaultOptions : array<string|int, mixed> = []
-
Default request's options
- $maxHostConnections : int = 6
-
The maximum number of connections to open
Tags
Return values
mixed —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
ResponseInterface —setLogger()
Sets a logger.
public
setLogger(LoggerInterface $logger) : mixed
Parameters
- $logger : LoggerInterface
Return values
mixed —stream()
Yields responses chunk by chunk as they complete.
public
stream(mixed $responses[, float $timeout = null ]) : ResponseStreamInterface
Parameters
- $responses : mixed
-
One or more responses created by the current HTTP client
- $timeout : float = null
-
The idle timeout before yielding timeout chunks
Return values
ResponseStreamInterface —configureHeadersAndProxy()
private
static configureHeadersAndProxy(mixed $context, string $host, array<string|int, mixed> $requestHeaders, array<string|int, mixed>|null $proxy, array<string|int, mixed> $noProxy, bool $isSsl) : bool
Parameters
- $context : mixed
- $host : string
- $requestHeaders : array<string|int, mixed>
- $proxy : array<string|int, mixed>|null
- $noProxy : array<string|int, mixed>
- $isSsl : bool
Return values
bool —createRedirectResolver()
Handles redirects - the native logic is too buggy to be used.
private
static createRedirectResolver(array<string|int, mixed> $options, string $host, array<string|int, mixed>|null $proxy, array<string|int, mixed> $noProxy, array<string|int, mixed> &$info, Closure|null $onProgress) : Closure
Parameters
- $options : array<string|int, mixed>
- $host : string
- $proxy : array<string|int, mixed>|null
- $noProxy : array<string|int, mixed>
- $info : array<string|int, mixed>
- $onProgress : Closure|null
Return values
Closure —dnsResolve()
Resolves the IP of the host using the local DNS cache if possible.
private
static dnsResolve(mixed $host, NativeClientState $multi, array<string|int, mixed> &$info, Closure|null $onProgress) : string
Parameters
- $host : mixed
- $multi : NativeClientState
- $info : array<string|int, mixed>
- $onProgress : Closure|null
Return values
string —getBodyAsString()
private
static getBodyAsString(mixed $body) : string
Parameters
- $body : mixed
Return values
string —getProxy()
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) : array<string|int, mixed>|null
Parameters
- $proxy : string|null
- $url : array<string|int, mixed>
Return values
array<string|int, mixed>|null —jsonEncode()
private
static jsonEncode(mixed $value[, int $flags = null ][, int $maxDepth = 512 ]) : string
Parameters
- $value : mixed
- $flags : int = null
- $maxDepth : int = 512
Tags
Return values
string —mergeDefaultOptions()
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|null —normalizeBody()
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|Closure —normalizeHeaders()
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> —parseHostPort()
Extracts the host and the port from the URL.
private
static parseHostPort(array<string|int, mixed> $url, array<string|int, mixed> &$info) : array<string|int, mixed>
Parameters
- $url : array<string|int, mixed>
- $info : array<string|int, mixed>
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> —removeDotSegments()
Removes dot-segments from a path.
private
static removeDotSegments(string $path) : mixed
Parameters
- $path : string
Tags
Return values
mixed —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>