64 if ( isset(
$options[
'caBundlePath'] ) ) {
65 $this->caBundlePath =
$options[
'caBundlePath'];
66 if ( !file_exists( $this->caBundlePath ) ) {
67 throw new Exception(
"Cannot find CA bundle: " . $this->caBundlePath );
70 static $opts =
array(
'connTimeout',
'reqTimeout',
'usePipelining',
'maxConnsPerHost' );
71 foreach ( $opts
as $key ) {
99 return $req[0][
'response'];
132 foreach ( $reqs
as $index => &$req ) {
133 $req[
'response'] =
array(
136 'headers' =>
array(),
140 if ( isset( $req[0] ) ) {
141 $req[
'method'] = $req[0];
144 if ( isset( $req[1] ) ) {
145 $req[
'url'] = $req[1];
148 if ( !isset( $req[
'method'] ) ) {
149 throw new Exception(
"Request has no 'method' field set." );
150 } elseif ( !isset( $req[
'url'] ) ) {
151 throw new Exception(
"Request has no 'url' field set." );
153 $req[
'query'] = isset( $req[
'query'] ) ? $req[
'query'] :
array();
155 if ( isset( $req[
'headers'] ) ) {
160 $req[
'headers'] = $headers;
161 if ( !isset( $req[
'body'] ) ) {
163 $req[
'headers'][
'content-length'] = 0;
166 if ( count( $reqs ) > 1 ) {
168 curl_setopt( $handles[$index], CURLOPT_FORBID_REUSE,
true );
173 $indexes = array_keys( $reqs );
174 if ( function_exists(
'curl_multi_setopt' ) ) {
175 if ( isset( $opts[
'usePipelining'] ) ) {
176 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$opts[
'usePipelining'] );
178 if ( isset( $opts[
'maxConnsPerHost'] ) ) {
180 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$opts[
'maxConnsPerHost'] );
185 $batches = array_chunk( $indexes, $this->maxConnsPerHost );
190 curl_multi_add_handle( $chm, $handles[$index] );
197 $mrc = curl_multi_exec( $chm, $active );
198 }
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
200 if ( $active > 0 && $mrc == CURLM_OK ) {
201 if ( curl_multi_select( $chm, 10 ) == -1 ) {
206 }
while ( $active > 0 && $mrc == CURLM_OK );
210 foreach ( $reqs
as $index => &$req ) {
211 $ch = $handles[$index];
212 curl_multi_remove_handle( $chm, $ch );
213 if ( curl_errno( $ch ) !== 0 ) {
214 $req[
'response'][
'error'] =
"(curl error: " .
215 curl_errno( $ch ) .
") " . curl_error( $ch );
218 $req[
'response'][0] = $req[
'response'][
'code'];
219 $req[
'response'][1] = $req[
'response'][
'reason'];
220 $req[
'response'][2] = $req[
'response'][
'headers'];
221 $req[
'response'][3] = $req[
'response'][
'body'];
222 $req[
'response'][4] = $req[
'response'][
'error'];
225 if ( isset( $req[
'_closeHandle'] ) ) {
226 fclose( $req[
'_closeHandle'] );
227 unset( $req[
'_closeHandle'] );
233 if ( function_exists(
'curl_multi_setopt' ) ) {
234 curl_multi_setopt( $chm, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
235 curl_multi_setopt( $chm, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
251 curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT,
252 isset( $opts[
'connTimeout'] ) ? $opts[
'connTimeout'] : $this->connTimeout );
253 curl_setopt( $ch, CURLOPT_TIMEOUT,
254 isset( $opts[
'reqTimeout'] ) ? $opts[
'reqTimeout'] : $this->reqTimeout );
255 curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
256 curl_setopt( $ch, CURLOPT_MAXREDIRS, 4 );
257 curl_setopt( $ch, CURLOPT_HEADER, 0 );
258 if ( !is_null( $this->caBundlePath ) ) {
259 curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER,
true );
260 curl_setopt( $ch, CURLOPT_CAINFO, $this->caBundlePath );
262 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
269 http_build_query( $req[
'query'],
'',
'&' )
272 $url .= strpos( $req[
'url'],
'?' ) ===
false ?
"?$query" :
"&$query";
274 curl_setopt( $ch, CURLOPT_URL, $url );
276 curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $req[
'method'] );
277 if ( $req[
'method'] ===
'HEAD' ) {
278 curl_setopt( $ch, CURLOPT_NOBODY, 1 );
281 if ( $req[
'method'] ===
'PUT' ) {
282 curl_setopt( $ch, CURLOPT_PUT, 1 );
283 if ( is_resource( $req[
'body'] ) ) {
284 curl_setopt( $ch, CURLOPT_INFILE, $req[
'body'] );
285 if ( isset( $req[
'headers'][
'content-length'] ) ) {
286 curl_setopt( $ch, CURLOPT_INFILESIZE, $req[
'headers'][
'content-length'] );
287 } elseif ( isset( $req[
'headers'][
'transfer-encoding'] ) &&
288 $req[
'headers'][
'transfer-encoding'] ===
'chunks'
290 curl_setopt( $ch, CURLOPT_UPLOAD,
true );
292 throw new Exception(
"Missing 'Content-Length' or 'Transfer-Encoding' header." );
294 } elseif ( $req[
'body'] !==
'' ) {
295 $fp = fopen(
"php://temp",
"wb+" );
296 fwrite( $fp, $req[
'body'], strlen( $req[
'body'] ) );
298 curl_setopt( $ch, CURLOPT_INFILE, $fp );
299 curl_setopt( $ch, CURLOPT_INFILESIZE, strlen( $req[
'body'] ) );
300 $req[
'_closeHandle'] = $fp;
302 curl_setopt( $ch, CURLOPT_INFILESIZE, 0 );
304 curl_setopt( $ch, CURLOPT_READFUNCTION,
305 function ( $ch, $fd, $length ) {
306 $data = fread( $fd, $length );
307 $len = strlen( $data );
311 } elseif ( $req[
'method'] ===
'POST' ) {
312 curl_setopt( $ch, CURLOPT_POST, 1 );
313 curl_setopt( $ch, CURLOPT_POSTFIELDS, $req[
'body'] );
315 if ( is_resource( $req[
'body'] ) || $req[
'body'] !==
'' ) {
316 throw new Exception(
"HTTP body specified for a non PUT/POST request." );
318 $req[
'headers'][
'content-length'] = 0;
323 if ( strpos(
$name,
': ' ) ) {
324 throw new Exception(
"Headers cannot have ':' in the name." );
328 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
330 curl_setopt( $ch, CURLOPT_HEADERFUNCTION,
331 function ( $ch, $header ) use ( &$req ) {
332 $length = strlen( $header );
334 if ( preg_match(
"/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header,
$matches ) ) {
335 $req[
'response'][
'code'] = (int)
$matches[2];
336 $req[
'response'][
'reason'] = trim(
$matches[3] );
339 if ( strpos( $header,
":" ) ===
false ) {
343 $req[
'response'][
'headers'][strtolower(
$name )] = trim(
$value );
348 if ( isset( $req[
'stream'] ) ) {
352 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
353 function ( $ch, $data ) use ( &$req ) {
354 return fwrite( $req[
'stream'], $data );
358 curl_setopt( $ch, CURLOPT_WRITEFUNCTION,
359 function ( $ch, $data ) use ( &$req ) {
360 $req[
'response'][
'body'] .= $data;
361 return strlen( $data );
373 if ( !$this->multiHandle ) {
374 $cmh = curl_multi_init();
375 if ( function_exists(
'curl_multi_setopt' ) ) {
376 curl_multi_setopt( $cmh, CURLMOPT_PIPELINING, (
int)$this->usePipelining );
377 curl_multi_setopt( $cmh, CURLMOPT_MAXCONNECTS, (
int)$this->maxConnsPerHost );
379 $this->multiHandle = $cmh;
385 if ( $this->multiHandle ) {
386 curl_multi_close( $this->multiHandle );