142 foreach ( $reqs
as $index => &
$req ) {
150 if ( isset(
$req[0] ) ) {
154 if ( isset(
$req[1] ) ) {
158 if ( !isset(
$req[
'method'] ) ) {
159 throw new Exception(
"Request has no 'method' field set." );
160 } elseif ( !isset(
$req[
'url'] ) ) {
161 throw new Exception(
"Request has no 'url' field set." );
163 $req[
'query'] = isset(
$req[
'query'] ) ?
$req[
'query'] : [];
165 if ( isset(
$req[
'headers'] ) ) {
170 $req[
'headers'] = $headers;
171 if ( !isset(
$req[
'body'] ) ) {
173 $req[
'headers'][
'content-length'] = 0;
176 if ( count( $reqs ) > 1 ) {
178 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE,
true );
183 $indexes = array_keys( $reqs );
184 if ( function_exists(
'curl_multi_setopt' ) ) {
185 if ( isset( $opts[
'usePipelining'] ) ) {
186 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$opts[
'usePipelining'] );
188 if ( isset( $opts[
'maxConnsPerHost'] ) ) {
190 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$opts[
'maxConnsPerHost'] );
195 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
201 curl_multi_add_handle( $chm, $handles[$index] );
208 $mrc = curl_multi_exec( $chm, $active );
209 $info = curl_multi_info_read( $chm );
210 if ( $info !==
false ) {
211 $infos[(int)$info[
'handle']] = $info;
213 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
215 if ( $active > 0 && $mrc == CURLM_OK ) {
216 if ( curl_multi_select( $chm, 10 ) == -1 ) {
221 }
while ( $active > 0 && $mrc == CURLM_OK );
225 foreach ( $reqs
as $index => &
$req ) {
226 $ch = $handles[$index];
227 curl_multi_remove_handle( $chm, $ch );
229 if ( isset( $infos[(
int)$ch] ) ) {
230 $info = $infos[(int)$ch];
231 $errno = $info[
'result'];
232 if ( $errno !== 0 ) {
233 $req[
'response'][
'error'] =
"(curl error: $errno)";
234 if ( function_exists(
'curl_strerror' ) ) {
235 $req[
'response'][
'error'] .=
" " . curl_strerror( $errno );
239 $req[
'response'][
'error'] =
"(curl error: no status set)";
243 $req[
'response'][0] =
$req[
'response'][
'code'];
244 $req[
'response'][1] =
$req[
'response'][
'reason'];
245 $req[
'response'][2] =
$req[
'response'][
'headers'];
246 $req[
'response'][3] =
$req[
'response'][
'body'];
247 $req[
'response'][4] =
$req[
'response'][
'error'];
250 if ( isset(
$req[
'_closeHandle'] ) ) {
251 fclose(
$req[
'_closeHandle'] );
252 unset(
$req[
'_closeHandle'] );
258 if ( function_exists(
'curl_multi_setopt' ) ) {
259 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
260 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
277 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT,
280 curl_setopt( $ch, CURLOPT_TIMEOUT,
282 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
283 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
284 curl_setopt( $ch, CURLOPT_HEADER, 0 );
285 if ( !is_null( $this->caBundlePath ) ) {
286 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
287 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
289 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
296 http_build_query(
$req[
'query'],
'',
'&' )
299 $url .= strpos(
$req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
301 curl_setopt( $ch, CURLOPT_URL, $url );
303 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST,
$req[
'method'] );
304 if (
$req[
'method'] ===
'HEAD' ) {
305 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
308 if (
$req[
'method'] ===
'PUT' ) {
309 curl_setopt( $ch, CURLOPT_PUT, 1 );
310 if ( is_resource(
$req[
'body'] ) ) {
311 curl_setopt( $ch, CURLOPT_INFILE,
$req[
'body'] );
312 if ( isset(
$req[
'headers'][
'content-length'] ) ) {
313 curl_setopt( $ch, CURLOPT_INFILESIZE,
$req[
'headers'][
'content-length'] );
314 } elseif ( isset(
$req[
'headers'][
'transfer-encoding'] ) &&
315 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
317 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
319 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
321 } elseif (
$req[
'body'] !==
'' ) {
322 $fp = fopen(
"php://temp",
"wb+" );
323 fwrite( $fp,
$req[
'body'], strlen(
$req[
'body'] ) );
325 curl_setopt( $ch, CURLOPT_INFILE, $fp );
326 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen(
$req[
'body'] ) );
327 $req[
'_closeHandle'] = $fp;
329 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
331 curl_setopt( $ch, CURLOPT_READFUNCTION,
332 function ( $ch, $fd, $length ) {
333 $data = fread( $fd, $length );
334 $len = strlen( $data );
338 } elseif (
$req[
'method'] ===
'POST' ) {
339 curl_setopt( $ch, CURLOPT_POST, 1 );
345 if ( defined(
'CURLOPT_SAFE_UPLOAD' ) ) {
346 curl_setopt( $ch, CURLOPT_SAFE_UPLOAD,
true );
347 } elseif ( is_array(
$req[
'body'] ) ) {
353 curl_setopt( $ch, CURLOPT_POSTFIELDS,
$req[
'body'] );
355 if ( is_resource(
$req[
'body'] ) ||
$req[
'body'] !==
'' ) {
356 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
358 $req[
'headers'][
'content-length'] = 0;
361 if ( !isset(
$req[
'headers'][
'user-agent'] ) ) {
367 if ( strpos(
$name,
': ' ) ) {
368 throw new Exception(
"Headers cannot have ':' in the name." );
372 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
374 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
375 function ( $ch, $header )
use ( &
$req ) {
376 $length = strlen( $header );
378 if ( preg_match(
"/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header,
$matches ) ) {
383 if ( strpos( $header,
":" ) ===
false ) {
392 if ( isset(
$req[
'stream'] ) ) {
396 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
397 function ( $ch, $data )
use ( &
$req ) {
398 return fwrite(
$req[
'stream'], $data );
402 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
403 function ( $ch, $data )
use ( &
$req ) {
404 $req[
'response'][
'body'] .= $data;
405 return strlen( $data );
417 if ( !$this->multiHandle ) {
418 $cmh = curl_multi_init();
419 if ( function_exists(
'curl_multi_setopt' ) ) {
420 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
421 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
423 $this->multiHandle = $cmh;