WP_Http_Streams::request( string $url, string|array $args = array() ): array|WP_Error
- Since
- 2.7.0, 3.7.0
- Source
wp-includes/class-wp-http-streams.php:32
Compatibility
- WordPress
- since 3.7.0
- 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
$urlstring- The request URL.
$argsstring|arrayoptional- Override the defaults.Default:
array()
Return value
array|WP_Error- Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
Performance profile
How much work a call to WP_Http_Streams::request() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 177–280
- Plugin surface
- 2 hooks
- Called by
- 0
Reads or writes the filesystem via fwrite().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 609.
Third-party callbacks on 'https_local_ssl_verify', 'https_ssl_verify' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
fwrite()called directly
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Http_Streams::request() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && !$parsed_args | 177 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), stream_set_blocking(), fclose() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args | 200–205 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), __(), sprintf() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() | 217–248 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), feof(), ::WP_Http(), fclose(), ::WP_Http(), ::WP_Http() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && !feof() && $bytes_written_to_file !== false | 222–239 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fread(), fwrite(), fclose(), fclose(), __() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args | 225–259 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fclose(), fclose(), ::WP_Http(), ::WP_Http() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $redirect_response === false | 234–255 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), feof(), ::WP_Http(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && !feof() && $response && $bytes_written_to_file !== false | 235–249 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fread(), ::WP_Http(), fwrite(), fclose(), fclose(), __() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $redirect_response === false && !empty($processed_response) | 241–255 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), feof(), ::WP_Http(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $redirect_response === false | 241–262 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), feof(), ::WP_Http(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && $redirect_response === false | 242–266 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fclose(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $redirect_response === false && !empty($processed_response) | 248–262 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), feof(), ::WP_Http(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && $redirect_response === false && !empty($processed_response) | 249–266 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fclose(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http() |
4 further outcomes, up to 280 instructions
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && $redirect_response === false | 249–273 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fclose(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $redirect_response === false && !empty($processed_response) | 255–269 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), feof(), ::WP_Http(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && $redirect_response === false && !empty($processed_response) | 256–273 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fclose(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding() |
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && $parsed_args && $redirect_response === false && !empty($processed_response) | 263–280 | wp_parse_args(), ::WP_Http(), parse_url(), strtolower(), apply_filters(), ssl(), floor(), ->is_enabled(), stream_socket_client(), stream_set_timeout(), ->is_enabled(), strtoupper(), ->is_enabled(), ->use_authentication(), fwrite(), fopen(), feof(), fclose(), fclose(), ::WP_Http(), ::WP_Http(), ::WP_Http(), ::WP_Http_Encoding(), ::WP_Http_Encoding() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 609 | 177–280 | 73 | |
| 8.5 | 609 | 177–280 | 73 | |
| 8.4 | 609 | 177–280 | 73 | 23 fewer instructions than PHP 8.3 |
| 8.3 | 632 | 181–291 | 73 | |
| 8.2 | 632 | 181–291 | 73 | |
| 8.1 | 632 | 181–291 | 73 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 636 | 182–292 | 73 |
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.
Hooks and filters fired · 2
2 hooks fire while WP_Http_Streams::request() runs, in this order:
- apply_filters( https_local_ssl_verify )filterline 109 (+77 into the body)
Filters whether SSL should be verified for local HTTP API requests.
- apply_filters( https_ssl_verify )filterline 112 (+80 into the body)
Filters whether SSL should be verified for non-local requests.
Uses · 13
- wp_parse_args()Merges user defined arguments into defaults array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- __()Retrieves the translation of $text.
- WP_Http::buildCookieHeader()Takes the arguments for a ::request() and checks for the cookie array.
- WP_HTTP_Proxy::__construct()
- WP_Error::__construct()Initializes the error.
- WP_Http_Streams::verify_ssl_certificate()Verifies the received SSL certificate against its Common Names and subjectAltName fields.
- WP_Http::processResponse()Parses the responses and splits the parts into headers and body.
- WP_Http::processHeaders()Transforms header string into an array.
- WP_Http::handle_redirects()Handles an HTTP redirect and follows it if appropriate.
- WP_Http::chunkTransferDecode()Decodes chunk transfer-encoding, based off the HTTP 1.1 specification.
- WP_Http_Encoding::should_decode()Whether the content be decoded based on the headers.
Show all 13
- WP_Http_Encoding::decompress()Decompression of deflated string.
Source code
public function request( $url, $args = array() ) { $defaults = array( 'method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array(), 'decompress' => false, 'stream' => false, 'filename' => null, ); $parsed_args = wp_parse_args( $args, $defaults ); if ( isset( $parsed_args['headers']['User-Agent'] ) ) { $parsed_args['user-agent'] = $parsed_args['headers']['User-Agent']; unset( $parsed_args['headers']['User-Agent'] ); } elseif ( isset( $parsed_args['headers']['user-agent'] ) ) { $parsed_args['user-agent'] = $parsed_args['headers']['user-agent']; unset( $parsed_args['headers']['user-agent'] ); } // Construct Cookie: header if any cookies are set. WP_Http::buildCookieHeader( $parsed_args ); $parsed_url = parse_url( $url ); $connect_host = $parsed_url['host']; $secure_transport = ( 'ssl' === $parsed_url['scheme'] || 'https' === $parsed_url['scheme'] ); if ( ! isset( $parsed_url['port'] ) ) { if ( 'ssl' === $parsed_url['scheme'] || 'https' === $parsed_url['scheme'] ) { $parsed_url['port'] = 443; $secure_transport = true; } else { $parsed_url['port'] = 80; } } // Always pass a path, defaulting to the root in cases such as http://example.com. if ( ! isset( $parsed_url['path'] ) ) { $parsed_url['path'] = '/'; } if ( isset( $parsed_args['headers']['Host'] ) || isset( $parsed_args['headers']['host'] ) ) { $parsed_url['host'] = $parsed_args['headers']['Host'] ?? $parsed_args['headers']['host']; unset( $parsed_args['headers']['Host'], $parsed_args['headers']['host'] ); } /* * Certain versions of PHP have issues with 'localhost' and IPv6, It attempts to connect * to ::1, which fails when the server is not set up for it. For compatibility, always * connect to the IPv4 address. */ if ( 'localhost' === strtolower( $connect_host ) ) { $connect_host = '127.0.0.1'; } $connect_host = $secure_transport ? 'ssl://' . $connect_host : 'tcp://' . $connect_host; $is_local = isset( $parsed_args['local'] ) && $parsed_args['local']; $ssl_verify = isset( $parsed_args['sslverify'] ) && $parsed_args['sslverify']; if ( $is_local ) { /** * Filters whether SSL should be verified for local HTTP API requests. * * @since 2.8.0 * @since 5.1.0 The `$url` parameter was added. * * @param bool|string $ssl_verify Boolean to control whether to verify the SSL connection * or path to an SSL certificate. * @param string $url The request URL. */ $ssl_verify = apply_filters( 'https_local_ssl_verify', $ssl_verify, $url ); } elseif ( ! $is_local ) { /** This filter is documented in wp-includes/class-wp-http.php */Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-http-streams.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.