WpOrg\Requests
Requests::request_multiple()
Send multiple HTTP requests simultaneously
The $requests parameter takes an associative or indexed array of request fields. The key of each request can be used to match up the request with the returned data, or with the request passed into your multiple.request.complete callback.
The request fields value is an associative array with the following keys:
- url: Request URL Same as the $url parameter to
{@see \WpOrg\Requests\Requests::request()} (string, required)
- headers: Associative array of header fields. Same as the $headers
parameter to {@see \WpOrg\Requests\Requests::request()} (array, default: `array()`)
- data: Associative array of data fields or a string. Same as the
`$data` parameter to {@see \WpOrg\Requests\Requests::request()} (array|string, default: `array()`)
- type: HTTP request type (use \WpOrg\Requests\Requests constants). Same as the $type
parameter to {@see \WpOrg\Requests\Requests::request()} (string, default: `\WpOrg\Requests\Requests::GET`)
- cookies: Associative array of cookie name to value, or cookie jar.
(array|\WpOrg\Requests\Cookie\Jar)
If the $options parameter is specified, individual requests will inherit options from it. This can be used to use a single hooking system, or set all the types to \WpOrg\Requests\Requests::POST, for example.
In addition, the $options parameter takes the following global options:
- complete: A callback for when a request is complete. Takes two
parameters, a \WpOrg\Requests\Response/\WpOrg\Requests\Exception reference, and the ID from the request array (Note: this can also be overridden on a per-request basis, although that's a little silly) (callback)
Method of the class: Requests{}
No Hooks.
Return
Array
. Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object)
Usage
$result = Requests::request_multiple( $requests, $options );
- $requests(array) (required)
- Requests data (see description for more information)
- $options(array)
- Global and default options (see \WpOrg\Requests\Requests::request())
Default: []
Requests::request_multiple() Requests::request multiple code WP 6.6.2
public static function request_multiple($requests, $options = []) { if (InputValidator::has_array_access($requests) === false || InputValidator::is_iterable($requests) === false) { throw InvalidArgument::create(1, '$requests', 'array|ArrayAccess&Traversable', gettype($requests)); } if (is_array($options) === false) { throw InvalidArgument::create(2, '$options', 'array', gettype($options)); } $options = array_merge(self::get_default_options(true), $options); if (!empty($options['hooks'])) { $options['hooks']->register('transport.internal.parse_response', [static::class, 'parse_multiple']); if (!empty($options['complete'])) { $options['hooks']->register('multiple.request.complete', $options['complete']); } } foreach ($requests as $id => &$request) { if (!isset($request['headers'])) { $request['headers'] = []; } if (!isset($request['data'])) { $request['data'] = []; } if (!isset($request['type'])) { $request['type'] = self::GET; } if (!isset($request['options'])) { $request['options'] = $options; $request['options']['type'] = $request['type']; } else { if (empty($request['options']['type'])) { $request['options']['type'] = $request['type']; } $request['options'] = array_merge($options, $request['options']); } self::set_defaults($request['url'], $request['headers'], $request['data'], $request['type'], $request['options']); // Ensure we only hook in once if ($request['options']['hooks'] !== $options['hooks']) { $request['options']['hooks']->register('transport.internal.parse_response', [static::class, 'parse_multiple']); if (!empty($request['options']['complete'])) { $request['options']['hooks']->register('multiple.request.complete', $request['options']['complete']); } } } unset($request); if (!empty($options['transport'])) { $transport = $options['transport']; if (is_string($options['transport'])) { $transport = new $transport(); } } else { $transport = self::get_transport(); } $responses = $transport->request_multiple($requests, $options); foreach ($responses as $id => &$response) { // If our hook got messed with somehow, ensure we end up with the // correct response if (is_string($response)) { $request = $requests[$id]; self::parse_multiple($response, $request); $request['options']['hooks']->dispatch('multiple.request.complete', [&$response, $id]); } } return $responses; }