wppaste
WordPress

wp_get_http( string $url, string|bool $file_path = false, int $red = 1 ): WpOrg\Requests\Utility\CaseInsensitiveDictionary|false

Since
2.5.0
Source
wp-includes/deprecated.php:3673
Perform a HTTP HEAD or GET request.

Description

If $file_path is a writable filename, this will do a GET request and write the file to that path.

Parameters

$urlstring
URL to fetch.
$file_pathstring|booloptional
File path to write request to. Default false.Default: false
$redintoptional
The number of Redirects followed, Upon 5 being hit, returns false. Default 1.Default: 1

Return value

WpOrg\Requests\Utility\CaseInsensitiveDictionary|false
Headers on success, false on failure.

Performance profile

How much work a call to wp_get_http() 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
Expensive

Reaches an outbound HTTP request via wp_safe_remote_request().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
16–64

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 78.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • httpoutbound HTTP requestwp_safe_remote_request()called directly
  • filesystemfilesystem accessfopen()called directly
  • hookthird-party callbacksdo_action()one call below wp_get_http()

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 · 6 distinct outcomes

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

WhenInstructionsCalls it makes
$red16_deprecated_function(), set_time_limit()
!$red && is_wp_error()27–28_deprecated_function(), set_time_limit(), wp_safe_remote_request(), is_wp_error()
!$red && !is_wp_error()40–46_deprecated_function(), set_time_limit(), wp_safe_remote_request(), is_wp_error(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code()
!$red && !is_wp_error()46–52_deprecated_function(), set_time_limit(), wp_safe_remote_request(), is_wp_error(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code(), fopen()
!$red && !is_wp_error() && isset($headers)52–53_deprecated_function(), set_time_limit(), wp_safe_remote_request(), is_wp_error(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code(), wp_get_http()
!$red && !is_wp_error()58–64_deprecated_function(), set_time_limit(), wp_safe_remote_request(), is_wp_error(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code(), fopen(), wp_remote_retrieve_body(), fwrite(), fclose(), clearstatcache()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.47816–6483 fewer instructions than PHP 8.3
8.38116–678
8.28116–678
8.18116–678
7.48116–678

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

Used by · 1

Source code

function wp_get_http( $url, $file_path = false, $red = 1 ) {	_deprecated_function( __FUNCTION__, '4.4.0', 'WP_Http' ); 	// Add 60 seconds to the script timeout to ensure the remote request has enough time.	if ( function_exists( 'set_time_limit' ) ) {		@set_time_limit( 60 );	} 	if ( $red > 5 )		return false; 	$options = array();	$options['redirection'] = 5; 	if ( false == $file_path )		$options['method'] = 'HEAD';	else		$options['method'] = 'GET'; 	$response = wp_safe_remote_request( $url, $options ); 	if ( is_wp_error( $response ) )		return false; 	$headers = wp_remote_retrieve_headers( $response );	$headers['response'] = wp_remote_retrieve_response_code( $response ); 	// WP_HTTP no longer follows redirects for HEAD requests.	if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {		return wp_get_http( $headers['location'], $file_path, ++$red );	} 	if ( false == $file_path )		return $headers; 	// GET request - write it to the supplied filename.	$out_fp = fopen($file_path, 'w');	if ( !$out_fp )		return $headers; 	fwrite( $out_fp,  wp_remote_retrieve_body( $response ) );	fclose($out_fp);	clearstatcache(); 	return $headers;}

Changelog

Introduced in 2.5.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.

4.4.0
Deprecated. Use WP_Httpfrom the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/deprecated.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.