WP_SimplePie_File::__construct( string $url, int $timeout = 10, int $redirects = 5, string|array $headers = null, string $useragent = null, bool $force_fsockopen = false )
- Since
- 2.8.0, 3.2.0, 5.6.1
- Source
wp-includes/class-wp-simplepie-file.php:46
Compatibility
- WordPress
- since 5.6.1
- 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- Remote file URL.
$timeoutintoptional- How long the connection should stay open in seconds.
Default 10.Default:10 $redirectsintoptional- The number of allowed redirects. Default 5.Default:
5 $headersstring|arrayoptional- Array or string of headers to send with the request.
Default null.Default:null $useragentstringoptional- User-agent value sent. Default null.Default:
null $force_fsockopenbooloptional- Whether to force opening internet or unix domain socket connection or not. Default false.Default:
false
Performance profile
How much work a call to WP_SimplePie_File::__construct() 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
- 26–76
- Plugin surface
- None
- Called by
- 0
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 106.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- httpoutbound HTTP request
wp_safe_remote_request()called directly - hookthird-party callbacks
do_action()one call below WP_SimplePie_File::__construct()
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_SimplePie_File::__construct() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$url | 26 | none |
$url && is_wp_error() | 50–56 | ::SimplePie\\Misc(), wp_safe_remote_request(), is_wp_error(), ->get_error_message() |
$url && !is_wp_error() && !($value instanceof) | 64–71 | ::SimplePie\\Misc(), wp_safe_remote_request(), is_wp_error(), wp_remote_retrieve_headers(), wp_remote_retrieve_body(), wp_remote_retrieve_response_code() |
$url && !is_wp_error() && $value instanceof | 69–76 | ::SimplePie\\Misc(), wp_safe_remote_request(), is_wp_error(), wp_remote_retrieve_headers(), ->getAll(), wp_remote_retrieve_body(), wp_remote_retrieve_response_code() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 106 | 26–76 | 9 | |
| 8.5 | 106 | 26–76 | 9 | |
| 8.4 | 106 | 26–76 | 9 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 112 | 29–79 | 9 | |
| 8.2 | 112 | 29–79 | 9 | |
| 8.1 | 112 | 29–79 | 9 | |
| 7.4 | 112 | 29–79 | 9 |
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_body()Retrieves only the body from the raw response.
- wp_remote_retrieve_response_code()Retrieves only the response code from the raw response.
- \SimplePie\Misc::get_default_useragent()
Source code
public function __construct( $url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false ) { $this->url = $url; $this->timeout = $timeout; $this->redirects = $redirects; $this->headers = $headers; $this->useragent = $useragent; $this->method = SimplePie\SimplePie::FILE_SOURCE_REMOTE; if ( preg_match( '/^http(s)?:\/\//i', $url ) ) { $args = array( 'timeout' => $this->timeout, 'redirection' => $this->redirects, ); if ( ! empty( $this->headers ) ) { $args['headers'] = $this->headers; } if ( SimplePie\Misc::get_default_useragent() !== $this->useragent ) { // Use default WP user agent unless custom has been specified. $args['user-agent'] = $this->useragent; } $res = wp_safe_remote_request( $url, $args ); if ( is_wp_error( $res ) ) { $this->error = 'WP HTTP Error: ' . $res->get_error_message(); $this->success = false; } else { $this->headers = wp_remote_retrieve_headers( $res ); if ( $this->headers instanceof \WpOrg\Requests\Utility\CaseInsensitiveDictionary ) { $this->headers = $this->headers->getAll(); } /* * SimplePie expects multiple headers to be stored as a comma-separated string, * but `wp_remote_retrieve_headers()` returns them as an array, so they need * to be converted. * * The only exception to that is the `content-type` header, which should ignore * any previous values and only use the last one. * * @see SimplePie\HTTP\Parser::new_line(). */ foreach ( $this->headers as $name => $value ) { if ( ! is_array( $value ) ) { continue; } if ( 'content-type' === $name ) { $this->headers[ $name ] = array_pop( $value ); } else { $this->headers[ $name ] = implode( ', ', $value ); } } $this->body = wp_remote_retrieve_body( $res ); $this->status_code = wp_remote_retrieve_response_code( $res ); } } else { $this->error = ''; $this->success = false; } }Changelog
Introduced in 2.8.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-simplepie-file.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.