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 55 of file MultiHttpClient.php.
MultiHttpClient::getCurlHandle |
( |
array & |
$req, |
|
|
array |
$opts |
|
) |
| |
|
protected |
- Parameters
-
array | &$req | HTTP request map |
-param array{url:string,proxy?:?string,query:mixed,method:string,body:string|resource,headers:string[],stream?:resource,flags:array} $req
- Parameters
-
array | $opts |
- connTimeout : default connection timeout
- reqTimeout : default request timeout
|
- Returns
- resource
- Exceptions
-
Definition at line 305 of file MultiHttpClient.php.
References $header, and $matches.
Referenced by runMultiCurl().
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:
list( $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[] | $reqs | Map of HTTP request arrays |
array | $opts | Options
- 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)
|
- Returns
- array[] $reqs With response array populated for each
- Exceptions
-
Definition at line 177 of file MultiHttpClient.php.
References isCurlEnabled(), normalizeRequests(), runMultiCurl(), and runMultiHttp().
Referenced by run().
MultiHttpClient::runMultiCurl |
( |
array |
$reqs, |
|
|
array |
$opts |
|
) |
| |
|
private |
Execute a set of HTTP(S) requests concurrently.
- See also
- MultiHttpClient::runMulti()
- Parameters
-
array[] | $reqs | Map of HTTP request arrays |
array | $opts |
- connTimeout : connection timeout per request (seconds)
- reqTimeout : post-connection timeout per request (seconds)
- usePipelining : whether to use HTTP pipelining if possible
- maxConnsPerHost : maximum number of concurrent connections (per host)
|
-param array{connTimeout?:int,reqTimeout?:int,usePipelining?:bool,maxConnsPerHost?:int} $opts
- Returns
- array $reqs With response array populated for each
- Exceptions
-
Exception | PhanTypeInvalidDimOffset |
Definition at line 224 of file MultiHttpClient.php.
References getCurlHandle(), getCurlMulti(), and getSelectTimeout().
Referenced by runMulti().
MultiHttpClient::runMultiHttp |
( |
array |
$reqs, |
|
|
array |
$opts = [] |
|
) |
| |
|
private |
Execute a set of HTTP(S) requests sequentially.
- See also
- MultiHttpClient::runMulti()
- Todo:
- Remove dependency on MediaWikiServices: use a separate HTTP client library or copy code from PhpHttpRequest
- Parameters
-
array | $reqs | Map of HTTP request arrays |
-param array<int,array{url:string,query:array,method:string,body:string,proxy?:?string,headers?:string[]}> $reqs
- Parameters
-
array | $opts |
- connTimeout : connection timeout per request (seconds)
- reqTimeout : post-connection timeout per request (seconds) -param array{connTimeout:int,reqTimeout:int} $opts
|
- Returns
- array $reqs With response array populated for each
- Exceptions
-
Definition at line 493 of file MultiHttpClient.php.
Referenced by runMulti().