MediaWiki  master
MultiHttpClient Class Reference

Class to handle multiple HTTP requests. More...

Inheritance diagram for MultiHttpClient:
Collaboration diagram for MultiHttpClient:

Public Member Functions

 __construct (array $options)
 Since 1.35, callers should use HttpRequestFactory::createMultiClient() to get a client object with appropriately configured timeouts instead of constructing a MultiHttpClient directly. More...
 
 __destruct ()
 
 run (array $req, array $opts=[])
 Execute an HTTP(S) request. More...
 
 runMulti (array $reqs, array $opts=[])
 Execute a set of HTTP(S) requests. More...
 
 setLogger (LoggerInterface $logger)
 Register a logger. More...
 

Protected Member Functions

 getCurlHandle (array &$req, array $opts)
 
 getCurlMulti (array $opts)
 
 isCurlEnabled ()
 Determines if the curl extension is available. More...
 

Protected Attributes

string null $caBundlePath
 SSL certificates path. More...
 
resource object null $cmh = null
 @phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.ObjectTypeHintVar curl_multi_init() handle, initialized in getCurlMulti() More...
 
float $connTimeout = 10
 
array array $headers = []
 
string false $localProxy = false
 
string[] $localVirtualHosts = []
 
LoggerInterface $logger
 
int $maxConnsPerHost = 50
 
float $maxConnTimeout = INF
 
float $maxReqTimeout = INF
 
string null $proxy
 proxy More...
 
float $reqTimeout = 30
 
bool $usePipelining = false
 
string $userAgent = 'wikimedia/multi-http-client v1.1'
 

Detailed Description

Class to handle multiple HTTP requests.

If curl is available, requests will be made concurrently. Otherwise, they will be made serially.

HTTP request maps are arrays that use the following format:

  • method : GET/HEAD/PUT/POST/DELETE
  • url : HTTP/HTTPS URL
  • query : <query parameter field/value associative array> (uses RFC 3986)
  • headers : <header name/value associative array>
  • body : source to get the HTTP request body from; this can simply be a string (always), a resource for PUT requests, and a field/value array for POST request; array bodies are encoded as multipart/form-data and strings use application/x-www-form-urlencoded (headers sent automatically)
  • stream : resource to stream the HTTP response body to
  • proxy : HTTP proxy to use
  • flags : map of boolean flags which supports:
    • relayResponseHeaders : write out header via header() Request maps can use integer index 0 instead of 'method' and 1 instead of 'url'.

Since 1.35, callers should use HttpRequestFactory::createMultiClient() to get a client object with appropriately configured timeouts.

Since
1.23

Definition at line 56 of file MultiHttpClient.php.

Constructor & Destructor Documentation

◆ __construct()

MultiHttpClient::__construct ( array  $options)

Since 1.35, callers should use HttpRequestFactory::createMultiClient() to get a client object with appropriately configured timeouts instead of constructing a MultiHttpClient directly.

Parameters
array$options
  • connTimeout : default connection timeout (seconds)
  • reqTimeout : default request timeout (seconds)
  • maxConnTimeout : maximum connection timeout (seconds)
  • maxReqTimeout : maximum request timeout (seconds)
  • proxy : HTTP proxy to use
  • localProxy : Reverse proxy to use for domains in localVirtualHosts
  • localVirtualHosts : Domains that are configured as virtual hosts on the same machine
  • usePipelining : whether to use HTTP pipelining if possible (for all hosts)
  • maxConnsPerHost : maximum number of concurrent connections (per host)
  • userAgent : The User-Agent header value to send
  • logger : a \Psr\Log\LoggerInterface instance for debug logging
  • caBundlePath : path to specific Certificate Authority bundle (if any)
  • headers : an array of default headers to send with every request
  • telemetry : a \Wikimedia\Http\RequestTelemetry instance to track telemetry data
Exceptions
Exception

Definition at line 120 of file MultiHttpClient.php.

◆ __destruct()

MultiHttpClient::__destruct ( )

Definition at line 807 of file MultiHttpClient.php.

Member Function Documentation

◆ getCurlHandle()

MultiHttpClient::getCurlHandle ( array &  $req,
array  $opts 
)
protected
Parameters
array&$reqHTTP request map @phpcs:ignore Generic.Files.LineLength
array$opts
  • connTimeout : default connection timeout
  • reqTimeout : default request timeout
  • httpVersion: default HTTP version @phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
Returns
resource|object
Exceptions
Exception

Definition at line 361 of file MultiHttpClient.php.

References $header, $headers, $matches, and $userAgent.

◆ getCurlMulti()

MultiHttpClient::getCurlMulti ( array  $opts)
protected
Parameters
array$opts@phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintReturn
Returns
resource|object
Exceptions
Exception

Definition at line 494 of file MultiHttpClient.php.

References $cmh, and $maxConnsPerHost.

◆ isCurlEnabled()

MultiHttpClient::isCurlEnabled ( )
protected

Determines if the curl extension is available.

Returns
bool true if curl is available, false otherwise.

Definition at line 236 of file MultiHttpClient.php.

Referenced by runMulti().

◆ run()

MultiHttpClient::run ( array  $req,
array  $opts = [] 
)

Execute an HTTP(S) request.

This method returns a response map of:

  • code : HTTP response code or 0 if there was a serious error
  • reason : HTTP response reason (empty if there was a serious error)
  • headers : <header name/value associative array>
  • body : HTTP response body or resource (if "stream" was set)
  • error : Any error string The map also stores integer-indexed copies of these values. This lets callers do:
    [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $http->run( $req );
    Parameters
    array$reqHTTP request array
    array$opts
  • connTimeout : connection timeout per request (seconds)
  • reqTimeout : post-connection timeout per request (seconds)
  • usePipelining : whether to use HTTP pipelining if possible (for all hosts)
  • maxConnsPerHost : maximum number of concurrent connections (per host)
  • httpVersion : One of 'v1.0', 'v1.1', 'v2' or 'v2.0'. Leave empty to use PHP/curl's default
    Returns
    array Response array for request

Definition at line 163 of file MultiHttpClient.php.

References runMulti().

◆ runMulti()

MultiHttpClient::runMulti ( array  $reqs,
array  $opts = [] 
)

Execute a set of HTTP(S) requests.

If curl is available, requests will be made concurrently. Otherwise, they will be made serially.

The maps are returned by this method with the 'response' field set to a map of:

  • code : HTTP response code or 0 if there was a serious error
  • reason : HTTP response reason (empty if there was a serious error)
  • headers : <header name/value associative array>
  • body : HTTP response body or resource (if "stream" was set)
  • error : Any error string The map also stores integer-indexed copies of these values. This lets callers do:
    [ $rcode, $rdesc, $rhdrs, $rbody, $rerr ] = $req['response'];
    All headers in the 'headers' field are normalized to use lower case names. This is true for the request headers and the response headers. Integer-indexed method/URL entries will also be changed to use the corresponding string keys.
Parameters
array[]$reqsMap of HTTP request arrays
array$optsOptions
  • connTimeout : connection timeout per request (seconds)
  • reqTimeout : post-connection timeout per request (seconds)
  • usePipelining : whether to use HTTP pipelining if possible (for all hosts)
  • maxConnsPerHost : maximum number of concurrent connections (per host)
  • httpVersion : One of 'v1.0', 'v1.1', 'v2' or 'v2.0'. Leave empty to use PHP/curl's default
Returns
array[] $reqs With response array populated for each
Exceptions
Exception

Definition at line 198 of file MultiHttpClient.php.

References $connTimeout, $maxConnTimeout, $maxReqTimeout, $reqTimeout, and isCurlEnabled().

Referenced by run().

◆ setLogger()

MultiHttpClient::setLogger ( LoggerInterface  $logger)

Register a logger.

Parameters
LoggerInterface$logger

Definition at line 803 of file MultiHttpClient.php.

Member Data Documentation

◆ $caBundlePath

string null MultiHttpClient::$caBundlePath
protected

SSL certificates path.

Definition at line 65 of file MultiHttpClient.php.

◆ $cmh

resource object null MultiHttpClient::$cmh = null
protected

@phpcs:ignore MediaWiki.Commenting.PropertyDocumentation.ObjectTypeHintVar curl_multi_init() handle, initialized in getCurlMulti()

Definition at line 63 of file MultiHttpClient.php.

Referenced by getCurlMulti().

◆ $connTimeout

float MultiHttpClient::$connTimeout = 10
protected

Definition at line 67 of file MultiHttpClient.php.

Referenced by runMulti().

◆ $headers

array array MultiHttpClient::$headers = []
protected

Definition at line 89 of file MultiHttpClient.php.

Referenced by getCurlHandle().

◆ $localProxy

string false MultiHttpClient::$localProxy = false
protected

Definition at line 81 of file MultiHttpClient.php.

◆ $localVirtualHosts

string [] MultiHttpClient::$localVirtualHosts = []
protected

Definition at line 83 of file MultiHttpClient.php.

◆ $logger

LoggerInterface MultiHttpClient::$logger
protected

Definition at line 87 of file MultiHttpClient.php.

◆ $maxConnsPerHost

int MultiHttpClient::$maxConnsPerHost = 50
protected

Definition at line 77 of file MultiHttpClient.php.

Referenced by getCurlMulti().

◆ $maxConnTimeout

float MultiHttpClient::$maxConnTimeout = INF
protected

Definition at line 69 of file MultiHttpClient.php.

Referenced by runMulti().

◆ $maxReqTimeout

float MultiHttpClient::$maxReqTimeout = INF
protected

Definition at line 73 of file MultiHttpClient.php.

Referenced by runMulti().

◆ $proxy

string null MultiHttpClient::$proxy
protected

proxy

Definition at line 79 of file MultiHttpClient.php.

◆ $reqTimeout

float MultiHttpClient::$reqTimeout = 30
protected

Definition at line 71 of file MultiHttpClient.php.

Referenced by runMulti().

◆ $usePipelining

bool MultiHttpClient::$usePipelining = false
protected

Definition at line 75 of file MultiHttpClient.php.

◆ $userAgent

string MultiHttpClient::$userAgent = 'wikimedia/multi-http-client v1.1'
protected

Definition at line 85 of file MultiHttpClient.php.

Referenced by getCurlHandle().


The documentation for this class was generated from the following file: