wppaste
WordPress

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
Send a HTTP request to a URI using PHP Streams.

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

Reads or writes the filesystem via fwrite().

Scaling
Scales with input

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

Instructions
177–280

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

Plugin surface
2 hooks

Third-party callbacks on 'https_local_ssl_verify', 'https_ssl_verify' run inside this call, and their cost is not bounded by anything here.

Called by
0

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfwrite()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.

WhenInstructionsCalls it makes
!isset($parsed_args) && $timeout === false && !->is_enabled() && $handle !== false && !->use_authentication() && !$parsed_args177wp_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_args200–205wp_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–248wp_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 !== false222–239wp_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_args225–259wp_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 === false234–255wp_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 !== false235–249wp_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–255wp_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 === false241–262wp_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 === false242–266wp_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–262wp_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–266wp_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 === false249–273wp_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–269wp_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–273wp_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–280wp_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

PHPCompiledExecutedBranchesNotes
8.6-dev609177–28073
8.5609177–28073
8.4609177–2807323 fewer instructions than PHP 8.3
8.3632181–29173
8.2632181–29173
8.1632181–291734 fewer instructions than PHP 7.4
7.4636182–29273

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:

  1. apply_filters( https_local_ssl_verify )filterline 109 (+77 into the body)

    Filters whether SSL should be verified for local HTTP API requests.

  2. apply_filters( https_ssl_verify )filterline 112 (+80 into the body)

    Filters whether SSL should be verified for non-local requests.

Uses · 13

Show all 13

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.

  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.

3.7.0
Combined with the fsockopen transport and switched to stream_socket_client().from the docblock
2.7.0
Introduced.from the docblock

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.