Skip to content

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

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

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

Conversation

@swissspidy

@swissspidy swissspidy commented Sep 22, 2026 •

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.

Related fixes in the Requests library

Working on this surfaced two issues in Transport\Curl::request_multiple(), which this function relies on when cURL is available:

This PR doesn't depend on either fix to work, but both affect how well batches behave in practice. They would reach core through a Requests release and the usual library update.


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.

This comment was marked as resolved.

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

This comment was marked as 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
swissspidy and others added 2 commits December 2, 2026 13:01
The cURL transport of the Requests library sends the requests of a batch
differently from a single request. Make up for the differences in
`WP_Http::request_multiple()`:

- Add the cookies of each request, which the Requests library adds in the
  `requests.before_request` hook that it does not fire for batches.
- Fire the `curl.before_send` hook for each request, through which the
  proxy settings are applied and the `http_api_curl` action fires.
- Apply the SSL verification settings, so that the `sslverify` and
  `sslcertificates` arguments and the `https_ssl_verify` filter are not
  ignored.
- Report cURL errors in the same format as for a single request.
- Fail a request that streams to a file that cannot be opened, or to the
  same file as another request of the batch, instead of reporting success
  without writing the file.

See #37459.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

⚠️ PHPStan Static Analysis

PHPStan reported 1 error when analyzing e3abd97 (workflow run). This comment is updated as new commits are pushed.

Errors

These are also shown as annotations on the changed files. They do not fail the check, but they should still be fixed before the pull request is committed. An error that is a false positive may instead warrant being added to a baseline.

Location Error Identifier First reported
src/wp-includes/class-wp-http.php:516 Class Stringable not found. class.notFound 64f1daa
Resolved errors (2)
Location Error Identifier First reported Resolved
src/wp-includes/class-wp-http.php:547 Parameter #3 $value of function curl_setopt expects bool, int given. argument.type a34eda8 3890d2a
src/wp-includes/class-wp-http.php:387 Negated boolean expression is always false. booleanNot.alwaysFalse a34eda8 3890d2a

Running PHPStan locally

To run the analysis on your own machine, use npm run typecheck:php, or composer phpstan outside the Docker environment.

swissspidy and others added 2 commits December 2, 2026 13:05
Pass a boolean to `CURLOPT_SSL_VERIFYPEER`, and explain to PHPStan that the
`requests.before_request` hook can change the URL that it already checked.

See #37459.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT
`Requests::request_multiple()` normalizes all requests of a batch before
sending any of them, and fails the whole batch when one of them cannot be
normalized, for example when its host cannot be encoded. Prepare each
request of a batch on its own in `WP_Http::request_multiple()` instead, as
`Requests::request()` does for a single request, so that such a request
only fails on its own. The `requests.before_request` hook now also receives
the same, normalized arguments as for a single request, and cookies are
matched against the encoded host.

Also make up for further differences between single and batched requests:

- Fire the `curl.after_send` and `requests.before_parse` hooks.
- Send a request again on its own when its response could not be
  decompressed or written, as the cURL transport does for a single
  request.
- Release the cURL handles of a batch, and the files that failed requests
  were streaming to, after each group of requests.
- Detect requests that stream to the same file through different paths,
  and do not truncate an existing file before its request is sent.

Make the new `prepare_request()` and `finalize_response()` methods private,
so that they cannot conflict with methods of `WP_Http` subclasses, and
document how the requests of a batch are grouped and sent.

See #37459.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UWTd5pYrbbBoL1571AjtuT
Documents that values returned by the `pre_http_request` filter are passed through unchanged by `wp_remote_request_multiple()`, `wp_safe_remote_request_multiple()` and `WP_Http::request_multiple()`.

Moves the test that opens a real connection into the `external-http` group.

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