148 public function runMulti( array $reqs, array $opts = [] ) {
153 foreach ( $reqs as $index => &
$req ) {
161 if ( isset(
$req[0] ) ) {
165 if ( isset(
$req[1] ) ) {
169 if ( !isset(
$req[
'method'] ) ) {
170 throw new Exception(
"Request has no 'method' field set." );
171 } elseif ( !isset(
$req[
'url'] ) ) {
172 throw new Exception(
"Request has no 'url' field set." );
174 $this->logger->debug(
"{$req['method']}: {$req['url']}" );
175 $req[
'query'] = isset(
$req[
'query'] ) ?
$req[
'query'] : [];
177 if ( isset(
$req[
'headers'] ) ) {
178 foreach (
$req[
'headers'] as $name =>
$value ) {
179 $headers[strtolower( $name )] =
$value;
182 $req[
'headers'] = $headers;
183 if ( !isset(
$req[
'body'] ) ) {
185 $req[
'headers'][
'content-length'] = 0;
187 $req[
'flags'] = isset(
$req[
'flags'] ) ?
$req[
'flags'] : [];
189 if ( count( $reqs ) > 1 ) {
191 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE,
true );
196 $indexes = array_keys( $reqs );
197 if ( isset( $opts[
'usePipelining'] ) ) {
198 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$opts[
'usePipelining'] );
200 if ( isset( $opts[
'maxConnsPerHost'] ) ) {
202 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$opts[
'maxConnsPerHost'] );
206 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
209 foreach ( $batches as
$batch ) {
211 foreach (
$batch as $index ) {
212 curl_multi_add_handle( $chm, $handles[$index] );
219 $mrc = curl_multi_exec( $chm, $active );
220 $info = curl_multi_info_read( $chm );
221 if ( $info !==
false ) {
222 $infos[(int)$info[
'handle']] = $info;
224 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
226 if ( $active > 0 && $mrc == CURLM_OK ) {
227 if ( curl_multi_select( $chm, 10 ) == -1 ) {
232 }
while ( $active > 0 && $mrc == CURLM_OK );
236 foreach ( $reqs as $index => &
$req ) {
237 $ch = $handles[$index];
238 curl_multi_remove_handle( $chm, $ch );
240 if ( isset( $infos[(
int)$ch] ) ) {
241 $info = $infos[(int)$ch];
242 $errno = $info[
'result'];
243 if ( $errno !== 0 ) {
244 $req[
'response'][
'error'] =
"(curl error: $errno)";
245 if ( function_exists(
'curl_strerror' ) ) {
246 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
248 $this->logger->warning(
"Error fetching URL \"{$req['url']}\": " .
249 $req[
'response'][
'error'] );
252 $req[
'response'][
'error'] =
"(curl error: no status set)";
256 $req[
'response'][0] =
$req[
'response'][
'code'];
257 $req[
'response'][1] =
$req[
'response'][
'reason'];
258 $req[
'response'][2] =
$req[
'response'][
'headers'];
259 $req[
'response'][3] =
$req[
'response'][
'body'];
260 $req[
'response'][4] =
$req[
'response'][
'error'];
263 if ( isset(
$req[
'_closeHandle'] ) ) {
264 fclose(
$req[
'_closeHandle'] );
265 unset(
$req[
'_closeHandle'] );
271 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
272 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
288 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT,
291 curl_setopt( $ch, CURLOPT_TIMEOUT,
293 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
294 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
295 curl_setopt( $ch, CURLOPT_HEADER, 0 );
296 if ( !is_null( $this->caBundlePath ) ) {
297 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
298 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
300 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
303 $query = http_build_query(
$req[
'query'],
'',
'&', PHP_QUERY_RFC3986 );
305 $url .= strpos(
$req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
307 curl_setopt( $ch, CURLOPT_URL, $url );
309 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST,
$req[
'method'] );
310 if (
$req[
'method'] ===
'HEAD' ) {
311 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
314 if (
$req[
'method'] ===
'PUT' ) {
315 curl_setopt( $ch, CURLOPT_PUT, 1 );
316 if ( is_resource(
$req[
'body'] ) ) {
317 curl_setopt( $ch, CURLOPT_INFILE,
$req[
'body'] );
318 if ( isset(
$req[
'headers'][
'content-length'] ) ) {
319 curl_setopt( $ch, CURLOPT_INFILESIZE,
$req[
'headers'][
'content-length'] );
320 } elseif ( isset(
$req[
'headers'][
'transfer-encoding'] ) &&
321 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
323 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
325 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
327 } elseif (
$req[
'body'] !==
'' ) {
328 $fp = fopen(
"php://temp",
"wb+" );
329 fwrite( $fp,
$req[
'body'], strlen(
$req[
'body'] ) );
331 curl_setopt( $ch, CURLOPT_INFILE, $fp );
332 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen(
$req[
'body'] ) );
333 $req[
'_closeHandle'] = $fp;
335 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
337 curl_setopt( $ch, CURLOPT_READFUNCTION,
338 function ( $ch, $fd, $length ) {
339 $data = fread( $fd, $length );
340 $len = strlen( $data );
344 } elseif (
$req[
'method'] ===
'POST' ) {
345 curl_setopt( $ch, CURLOPT_POST, 1 );
351 if ( defined(
'CURLOPT_SAFE_UPLOAD' ) ) {
352 curl_setopt( $ch, CURLOPT_SAFE_UPLOAD,
true );
353 } elseif ( is_array(
$req[
'body'] ) ) {
357 $req[
'body'] = http_build_query(
$req[
'body'] );
359 curl_setopt( $ch, CURLOPT_POSTFIELDS,
$req[
'body'] );
361 if ( is_resource(
$req[
'body'] ) ||
$req[
'body'] !==
'' ) {
362 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
364 $req[
'headers'][
'content-length'] = 0;
367 if ( !isset(
$req[
'headers'][
'user-agent'] ) ) {
372 foreach (
$req[
'headers'] as $name =>
$value ) {
373 if ( strpos( $name,
': ' ) ) {
374 throw new Exception(
"Headers cannot have ':' in the name." );
376 $headers[] = $name .
': ' . trim(
$value );
378 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
380 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
382 if ( !empty(
$req[
'flags'][
'relayResponseHeaders'] ) ) {
387 if ( preg_match(
"/^(HTTP\/1\.[01]) (\d{3}) (.*)/",
$header,
$matches ) ) {
392 if ( strpos(
$header,
":" ) ===
false ) {
396 $req[
'response'][
'headers'][strtolower( $name )] = trim(
$value );
401 if ( isset(
$req[
'stream'] ) ) {
405 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
406 function ( $ch, $data ) use ( &
$req ) {
407 return fwrite(
$req[
'stream'], $data );
411 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
412 function ( $ch, $data ) use ( &
$req ) {
413 $req[
'response'][
'body'] .= $data;
414 return strlen( $data );
426 if ( !$this->multiHandle ) {
427 $cmh = curl_multi_init();
428 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
429 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
430 $this->multiHandle = $cmh;