wppaste
WordPress

Requests::request_multiple( array $requests, array $options = [] ): array

Source
wp-includes/Requests/src/Requests.php:520
Send multiple HTTP requests simultaneously

Description

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 \WpOrg\Requests\Requests::request() (string, required)
  • headers: Associative array of header fields. Same as the $headers parameter to \WpOrg\Requests\Requests::request() (array, default: array())
  • data: Associative array of data fields or a string. Same as the $data parameter to \WpOrg\Requests\Requests::request() (array|string, default: array())
  • type: HTTP request type (use \WpOrg\Requests\Requests constants). Same as the $type parameter to \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)

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$requestsarray
Requests data (see description for more information)
$optionsarrayoptional
Global and default options (see \WpOrg\Requests\Requests::request())Default: []

Return value

array
Responses (either \WpOrg\Requests\Response or a \WpOrg\Requests\Exception object)

Performance profile

How much work a call to Requests::request_multiple() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
15–66

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 184.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Requests::request_multiple() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15::InputValidator(), ::InvalidArgument()
always20–23::InputValidator(), ::InputValidator(), ::InvalidArgument()
empty($options)41–53::InputValidator(), ::InputValidator(), ::get_default_options(), array_merge(), ::get_transport(), ->request_multiple()
always42–54::InputValidator(), ::InputValidator(), ::get_default_options(), array_merge(), ->request_multiple()
!empty($options)47–61::InputValidator(), ::InputValidator(), ::get_default_options(), array_merge(), ->register(), ->request_multiple()
always58–60::InputValidator(), ::InputValidator(), ::get_default_options(), array_merge(), ->register(), ::get_transport(), ->request_multiple()
!empty($options)64–66::InputValidator(), ::InputValidator(), ::get_default_options(), array_merge(), ->register(), ->register(), ->request_multiple()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 184 instructions, 15–66 executed per call, 19 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

  • \WpOrg\Requests\Utility\InputValidator::has_array_access()
  • \WpOrg\Requests\Utility\InputValidator::is_iterable()
  • \WpOrg\Requests\Exception\InvalidArgument::create()
  • \WpOrg\Requests\Requests::get_default_options()
  • \WpOrg\Requests\Requests::set_defaults()
  • \WpOrg\Requests\Requests::get_transport()
  • \WpOrg\Requests\Requests::parse_multiple()

Source code

	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;	}

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/Requests/src/Requests.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.