_fetch_remote_file( string $url, array $headers = "" ): Snoopy
- Since
- 1.5.0
- Source
wp-includes/rss.php:550
Compatibility
- WordPress
- since 1.5.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- URL to retrieve
$headersarrayoptional- Headers to send to the URL. Default empty string.Default:
""
Return value
Snoopy- style response
Performance profile
How much work a call to _fetch_remote_file() 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
- Scales with input
- Instructions
- 31–42
- Plugin surface
- None
- Called by
- 1
Reaches an outbound HTTP request via wp_safe_remote_request().
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 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 - hookthird-party callbacks
do_action()one call below _fetch_remote_file()
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _fetch_remote_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 31 | headers(), is_wp_error(), array_shift(), wp_safe_remote_request() |
!is_wp_error() | 41–42 | headers(), is_wp_error(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 78 instructions, 31–42 executed per call, 6 branches. The work does not change between versions.
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 · 6
- 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_remote_retrieve_body()Retrieves only the body from the raw response.
- stdClass::__construct()
Used by · 1
- fetch_rss()Build Magpie object based on RSS from URL.
Source code
function _fetch_remote_file($url, $headers = "" ) { $resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) ); if ( is_wp_error($resp) ) { $error = array_shift($resp->errors); $resp = new stdClass; $resp->status = 500; $resp->response_code = 500; $resp->error = $error[0] . "\n"; //\n = Snoopy compatibility return $resp; } // Snoopy returns headers unprocessed. // Also note, WP_HTTP lowercases all keys, Snoopy did not. $return_headers = array(); foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) { if ( !is_array($value) ) { $return_headers[] = "$key: $value"; } else { foreach ( $value as $v ) $return_headers[] = "$key: $v"; } } $response = new stdClass; $response->status = wp_remote_retrieve_response_code( $resp ); $response->response_code = wp_remote_retrieve_response_code( $resp ); $response->headers = $return_headers; $response->results = wp_remote_retrieve_body( $resp ); return $response;}Changelog
Introduced in 1.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 6.9.7 tag, from
src/wp-includes/rss.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.