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:3678
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
- Scaling
- Constant
- Instructions
- 16–64
- Plugin surface
- None
- Called by
- 1
Reaches an outbound HTTP request via wp_safe_remote_request().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 78.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- httpoutbound HTTP request
wp_safe_remote_request()called directly - filesystemfilesystem access
fopen()called directly - hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
$red | 16 | _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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.4 | 78 | 16–64 | 8 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 81 | 16–67 | 8 | |
| 8.2 | 81 | 16–67 | 8 | |
| 8.1 | 81 | 16–67 | 8 | |
| 7.4 | 81 | 16–67 | 8 |
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
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- wp_safe_remote_request()Retrieves the raw response from a safe HTTP request.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_remote_retrieve_headers()Retrieves only the headers from the raw response.
- wp_remote_retrieve_response_code()Retrieves only the response code from the raw response.
- wp_get_http()Perform a HTTP HEAD or GET request.
- wp_remote_retrieve_body()Retrieves only the body from the raw response.
Used by · 1
- wp_get_http()Perform a HTTP HEAD or GET request.
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.
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/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.