wppaste
WordPress

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
Constructor.

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

Reaches an outbound HTTP request via wp_safe_remote_request().

Scaling
Scales with input

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

Instructions
26–76

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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

What it touches

  • httpoutbound HTTP requestwp_safe_remote_request()called directly
  • hookthird-party callbacksdo_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.

WhenInstructionsCalls it makes
!$url26none
$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 instanceof69–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

PHPCompiledExecutedBranchesNotes
8.6-dev10626–769
8.510626–769
8.410626–7696 fewer instructions than PHP 8.3
8.311229–799
8.211229–799
8.111229–799
7.411229–799

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

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.

  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.

5.6.1
Multiple headers are concatenated into a comma-separated string, rather than remaining an array.from the docblock
3.2.0
Updated to use a PHP5 constructor.from the docblock
2.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.