Skip to content

HTTP API: Add wp_remote_request_multiple() for concurrent requests - #13657

Open
swissspidy wants to merge 16 commits into
WordPress:trunkfrom
swissspidy:trac-37459-http-request-multiple
Open

swissspidy wants to merge 16 commits into
WordPress:trunkfrom
swissspidy:trac-37459-http-request-multiple

Conversation

@swissspidy

Copy link
Copy Markdown
Member

Adds concurrent HTTP requests to the HTTP API, picking up where #3022 left off.

What this adds

  • WP_Http::request_multiple( array $requests, array $options = array() ), surfaced as wp_remote_request_multiple() and wp_safe_remote_request_multiple(). Each request is either a URL string or an array with url and optional args keys, keyed by an identifier of the caller's choosing; the responses come back keyed and ordered the same way. Every element is a normal response array or a WP_Error, so wp_remote_retrieve_*() work on each one.
  • WP_Http::request() is split into prepare_request() and finalize_response(), moved verbatim, so batched requests go through exactly the same pipeline as single ones: http_request_args, pre_http_request (a short-circuited request is answered without being sent), URL validation and reject_unsafe_urls, block_request(), proxies, https_ssl_verify, http_api_debug and http_response, each applied per request.

What changed compared to #3022

  • Dedicated functions instead of overloading $url. In Add additional layer in WP_Http to support parallel requests without requiring use of Requests directly #3022 wp_remote_request(), wp_remote_get(), wp_remote_post() and wp_remote_head() accepted an array of requests, which changed their return type by input type and ignored the $args parameter with a _doing_it_wrong(). That also meant wp_safe_remote_get() could not batch, because reject_unsafe_urls is passed through that parameter. The single-request functions are unchanged here.
  • A concurrency limit. Add additional layer in WP_Http to support parallel requests without requiring use of Requests directly #3022 handed the whole batch to Requests::request_multiple() at once. Batches are now sent at most concurrency requests at a time (default 6, settable per call and filterable with http_request_multiple_concurrency).
  • http_api_curl fires for batched requests. Requests dispatches curl.before_multi_add instead of curl.before_send for the requests of a batch, and WP_HTTP_Requests_Hooks only mapped the latter, so code hooking http_api_curl (proxy or resolver options, for example) silently did not apply to batches.
  • Per-request errors when Requests rejects a batch. Requests::request_multiple() throws for the whole batch when one request cannot be sent at all (a non-HTTP scheme, for instance). Add additional layer in WP_Http to support parallel requests without requiring use of Requests directly #3022 turned that into a single WP_Error return value. The batch is now retried one request at a time so each request gets its own response or error.
  • Non-blocking requests are performed in full, since the batch transport cannot do otherwise; the blocking argument is forced to true and the behaviour is documented.
  • Tests. Network-free tests for keying and ordering, per-request filtering, short-circuiting, invalid and non-HTTP URLs, the safe variant and forced blocking; external-http tests for real batches (redirects, HEAD, streaming to a file), per-response http_response/http_api_debug, the partial-batch fallback, http_api_curl for batched requests, and the concurrency option and filter.

Not in this PR

Nothing uses the new function in core yet. The natural first consumer is the upgrader: Plugin_Upgrader::bulk_upgrade() and friends download one package at a time and could fetch them all first with wp_safe_remote_request_multiple(). That is a separate change on top of this one.

Trac ticket: https://core.trac.wordpress.org/ticket/37459

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude (recorded in the commit's Co-Authored-By trailer)
Used for: The implementation and the tests were drafted with Claude Code from a review of #3022, and were reviewed and tested by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

swissspidy and others added 3 commits September 22, 2026 12:14
Introduce `WP_Http::request_multiple()`, `wp_remote_request_multiple()` and `wp_safe_remote_request_multiple()`, which send a batch of requests side by side and return their responses keyed and ordered like the requests.

`WP_Http::request()` is split into `prepare_request()` and `finalize_response()`, so a batched request goes through the same steps as a single one: the `http_request_args`, `pre_http_request` and `http_response` filters, URL validation, blocked hosts, proxies and the `http_api_debug` action all apply to each request individually.

Compared to the earlier patch on the ticket:

* The single-request functions keep their signatures instead of accepting an array of requests, so `reject_unsafe_urls` can still be passed for a batch.
* Batches are sent at most `concurrency` requests at a time (default 6, filterable with `http_request_multiple_concurrency`).
* The `http_api_curl` action fires for batched requests too. Requests dispatches `curl.before_multi_add` instead of `curl.before_send` for them, which `WP_HTTP_Requests_Hooks` did not map.
* When the Requests library rejects a batch as a whole, its requests are retried one by one so that each gets its own response or error.

See #37459.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvSBA4zz2vHphxunmJCV2G
* Catch `Exception` around `Requests::request_multiple()`. The Requests library only documents `InvalidArgument` on that method, although the URL validation and transport lookup it performs throw `WpOrg\Requests\Exception`, so PHPStan considered the narrower catch dead.
* Document the two shapes `WP_Http::prepare_request()` returns with a `@phpstan-return`, as hash notation cannot express a union of shapes.
* Describe the `$requests` parameter in prose. Its hash notation described a single request rather than the list of them.

See #37459.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvSBA4zz2vHphxunmJCV2G
`WP_Http::prepare_request()` signals a short-circuited request with the presence of the `response` key, but the callers checked it with `isset()`, which does not see a `null` value. A `pre_http_request` callback returning `null` therefore made `WP_Http::request()` read undefined keys and `WP_Http::request_multiple()` send a request without a URL, whereas `null` used to be returned to the caller unchanged.

Check for the key with `array_key_exists()` instead, and add a test for it.

See #37459.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8CZQKtzf87KYDehmhKnED
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@swissspidy
swissspidy marked this pull request as ready for review September 22, 2026 13:37
Copilot AI lite review requested due to automatic review settings September 22, 2026 13:37
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props swissspidy, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@swissspidy
swissspidy requested a review from rmccue September 22, 2026 13:37

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have much experience with Requests but I wanted to share a quick report on PHPStan issues being reported on these changed lines:


 ------ --------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/class-wp-http.php
 ------ --------------------------------------------------------------------------------------------------------------------------
  179    Method WP_Http::request() should return array{headers: array|WpOrg\Requests\Utility\CaseInsensitiveDictionary, body:
         string, response: array{code: int|false, message: string|false}, cookies: array<WP_Http_Cookie>, filename?: string|null,
         http_response: WP_HTTP_Requests_Response|null}|WP_Error but returns array|WP_Error.
         💡 Type #1 from the union: Sealed array shape can only accept a constant array. Extra keys are not allowed.
         🪪  return.type
  189    Parameter #3 $data of static method WpOrg\Requests\Requests::request() expects array|null, mixed given.
         🪪  argument.type
  199    Method WP_Http::request() should return array{headers: array|WpOrg\Requests\Utility\CaseInsensitiveDictionary, body:
         string, response: array{code: int|false, message: string|false}, cookies: array<WP_Http_Cookie>, filename?: string|null,
         http_response: WP_HTTP_Requests_Response|null}|WP_Error but returns array|WP_Error.
         💡 Type #1 from the union: Sealed array shape can only accept a constant array. Extra keys are not allowed.
         🪪  return.type
  230    Method WP_Http::request_multiple() has parameter $requests with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  230    Method WP_Http::request_multiple() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  240    Parameter #1 $args of function wp_parse_args expects array<string, mixed>|object|string, mixed given.
         🪪  argument.type
  245    Parameter #1 $url of method WP_Http::prepare_request() expects string, mixed given.
         🪪  argument.type
  302    Parameter #3 $data of static method WpOrg\Requests\Requests::request() expects array|null, mixed given.
         🪪  argument.type
  321    Parameter #1 $response of method WP_Http::finalize_response() expects
         WP_Error|WpOrg\Requests\Exception|WpOrg\Requests\Response, mixed given.
         🪪  argument.type
  361    Method WP_Http::prepare_request() has parameter $args with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  361    Method WP_Http::prepare_request() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  361    Method WP_Http::prepare_request() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  361    Method WP_Http::prepare_request() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  361    Method WP_Http::prepare_request() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  606    Method WP_Http::prepare_request() should return array{response: array|WP_Error}|array{url: string, headers: array, data:
         mixed, type: string, options: array, args: array}|WP_Error but returns array{url: non-falsy-string, headers:
         array<mixed>, data: mixed, type: mixed, options: array{timeout: mixed, useragent: mixed, blocking: mixed, hooks:
         WP_HTTP_Requests_Hooks, filename?: mixed, follow_redirects: false, max_bytes?: mixed, cookies?:
         WpOrg\Requests\Cookie\Jar, ...}|array{timeout: mixed, useragent: mixed, blocking: mixed, hooks: WP_HTTP_Requests_Hooks,
         filename?: mixed, redirects: mixed, max_bytes?: mixed, cookies?: WpOrg\Requests\Cookie\Jar, ...}, args:
         non-empty-array}.
         💡 • Type #1 from the union: Array does not have offset 'response'.
         • Type #2 from the union: Offset 'type' (string) does not accept type mixed.
         🪪  return.type
  630    Method WP_Http::finalize_response() has parameter $parsed_args with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  630    Method WP_Http::finalize_response() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  637    Parameter #2 $filename of class WP_HTTP_Requests_Response constructor expects string, mixed given.
         🪪  argument.type
 ------ --------------------------------------------------------------------------------------------------------------------------

 ------ --------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/http.php
 ------ --------------------------------------------------------------------------------------------------------------------------
  160    Function wp_safe_remote_request_multiple() has parameter $options with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  160    Function wp_safe_remote_request_multiple() has parameter $requests with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  160    Function wp_safe_remote_request_multiple() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  166    Parameter #1 $args of function wp_parse_args expects array<string, mixed>|object|string, mixed given.
         🪪  argument.type
  174    Parameter #2 $options of method WP_Http::request_multiple() expects array{concurrency?: int, ...}, array given.
         🪪  argument.type
  288    Function wp_remote_request_multiple() has parameter $options with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  288    Function wp_remote_request_multiple() has parameter $requests with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  288    Function wp_remote_request_multiple() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  290    Parameter #2 $options of method WP_Http::request_multiple() expects array{concurrency?: int, ...}, array given.
         🪪  argument.type
 ------ --------------------------------------------------------------------------------------------------------------------------

 [ERROR] Found 27 errors on changed lines (base: trunk) 


Comment thread src/wp-includes/class-wp-http-curl.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
swissspidy and others added 2 commits September 25, 2026 20:39
…e `http_api_curl` docblock.

See #37459.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8CZQKtzf87KYDehmhKnED
* Declare the response array as a `Response` type on `WP_Http` and use it for the return value of `request()`, `finalize_response()` and the short-circuit value `prepare_request()` returns, rather than describing the shape in each place.
* Declare the prepared request as a `Prepared_Request` type and give `request_multiple()`, `wp_remote_request_multiple()` and `wp_safe_remote_request_multiple()` value types for their requests, options and responses.
* Type the results the Requests library returns for a batch.
* Only parse request arguments given as a string, since arrays are passed on as they are.
* Align the parameter descriptions of `finalize_response()` and reference methods with `{@see}`.

See #37459.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01D8CZQKtzf87KYDehmhKnED
claude and others added 2 commits November 17, 2026 21:57
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Type the value of the 'pre_http_request' filter as a response, as documented, so that
a short-circuited request matches the method's return type. Loosen the Prepared_Request
shape to what prepare_request() actually guarantees, since its values come from the
filtered request arguments.

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
@swissspidy
swissspidy dismissed westonruter’s stale review November 17, 2026 22:07

stale, feedback addressed

Comment thread src/wp-includes/class-wp-http-curl.php Resolved
Comment thread src/wp-includes/http.php Resolved
Comment thread src/wp-includes/http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/http.php Resolved
Comment thread src/wp-includes/http.php Resolved
claude and others added 2 commits November 18, 2026 22:06
Update the `@since` tags for the new functions, methods, filter, and changelog entry
to 7.3.0. Add native parameter and return types to the new functions and methods.

WP_Http::prepare_request() now requires an array of arguments, so WP_Http::request()
parses string arguments before passing them on, as WP_Http::request_multiple() already
does. Its $url parameter stays untyped, so a URL that is not a string still produces
a WP_Error from WP_Http::request() rather than a TypeError.

Turn the comment about 'curl.before_multi_add' into a docblock that links to
WP_Http::request_multiple().

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
Reset the mbstring encoding in `finally` blocks, so that it is restored even when the
Requests library throws an exception that is not caught. Use the null coalescing
operator for optional request values and options. Simplify the PHPStan types by
dropping `array-key`, which adds no information.

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
@swissspidy
swissspidy requested review from mukeshpanchal27 and westonruter and removed request for johnbillion and ocean90 November 25, 2026 11:06
swissspidy and others added 2 commits November 25, 2026 11:17
# Conflicts:
#	src/wp-includes/class-wp-http.php
`WP_Http::request_multiple()` caught every exception thrown while sending
a batch and then sent each request of the batch again, one by one. Such an
exception can come from a callback on a hook that fires after the requests
were sent, so non-idempotent requests could be sent twice, and exceptions
unrelated to the Requests library were swallowed.

Reject non-HTTP(S) URLs per request up front instead, with the same error
that `Requests::request()` throws, and report a Requests exception thrown
for the batch as the error of each of its requests without resending them.

Also fire the `requests.before_request` hook for each batched request, as
`Requests::request()` does for a single request but
`Requests::request_multiple()` does not.

See #37459.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT
HTTP API: Merge trunk and fix batch resend in WP_Http::request_multiple()
Comment thread src/wp-includes/http.php Resolved
Comment thread src/wp-includes/http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Comment thread src/wp-includes/class-wp-http.php Resolved
Add `HTTP_Request_Args` and `Parsed_HTTP_Request_Args` PHPStan types and use
them for the request arguments of `WP_Http::request()`,
`WP_Http::request_multiple()`, `WP_Http::prepare_request()` and
`WP_Http::finalize_response()`. Match the return types of
`wp_remote_request_multiple()` and `wp_safe_remote_request_multiple()` to
`WP_Http::request_multiple()`, and document the concurrency passed to the
`http_request_multiple_concurrency` filter as a positive integer, clamping it
before the filter as well as after.

See #37459.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants