wppaste
WordPress

Requests::set_defaults( string $url, array $headers, array|null $data, string $type, array $options ): void

Source
wp-includes/Requests/src/Requests.php:656
Set the default values

Description

The $options parameter is updated with the results.

Compatibility

WordPress
core
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 request
$headersarray
Extra headers to send with the request
$dataarray|null
Data to send either as a query string for GET/HEAD requests, or in the body for POST requests
$typestring
HTTP request type
$optionsarray
Options for the request

Return value

void

Performance profile

How much work a call to Requests::set_defaults() 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
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
17–112

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

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 one call costs · 9 distinct outcomes

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

WhenInstructionsCalls it makes
!preg_match()17preg_match()
preg_match()46–81preg_match(), strtoupper()
preg_match()52–87preg_match(), ->register(), strtoupper()
preg_match()58–93preg_match(), ->register(), ->register(), strtoupper()
preg_match()59–94preg_match(), ::IdnaEncoder(), strtoupper()
preg_match()64–99preg_match(), ->register(), ->register(), ->register(), strtoupper()
preg_match()65–100preg_match(), ->register(), ::IdnaEncoder(), strtoupper()
preg_match()71–106preg_match(), ->register(), ->register(), ::IdnaEncoder(), strtoupper()
preg_match()77–112preg_match(), ->register(), ->register(), ->register(), ::IdnaEncoder(), strtoupper()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12717–11213
8.512717–11213
8.412717–112133 fewer instructions than PHP 8.3
8.313017–11513
8.213017–11513
8.113017–11513
7.413017–11513

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

  • \WpOrg\Requests\Exception::__construct()
  • \WpOrg\Requests\Hooks::__construct()
  • \WpOrg\Requests\Auth\Basic::__construct()
  • \WpOrg\Requests\Proxy\Http::__construct()
  • \WpOrg\Requests\Cookie\Jar::__construct()
  • \WpOrg\Requests\Iri::__construct()
  • \WpOrg\Requests\IdnaEncoder::encode()

Source code

	protected static function set_defaults(&$url, &$headers, &$data, &$type, &$options) {		if (!preg_match('/^http(s)?:\/\//i', $url, $matches)) {			throw new Exception('Only HTTP(S) requests are handled.', 'nonhttp', $url);		} 		if (empty($options['hooks'])) {			$options['hooks'] = new Hooks();		} 		if (is_array($options['auth'])) {			$options['auth'] = new Basic($options['auth']);		} 		if ($options['auth'] !== false) {			$options['auth']->register($options['hooks']);		} 		if (is_string($options['proxy']) || is_array($options['proxy'])) {			$options['proxy'] = new Http($options['proxy']);		} 		if ($options['proxy'] !== false) {			$options['proxy']->register($options['hooks']);		} 		if (is_array($options['cookies'])) {			$options['cookies'] = new Jar($options['cookies']);		} elseif (empty($options['cookies'])) {			$options['cookies'] = new Jar();		} 		if ($options['cookies'] !== false) {			$options['cookies']->register($options['hooks']);		} 		if ($options['idn'] !== false) {			$iri       = new Iri($url);			$iri->host = IdnaEncoder::encode($iri->ihost);			$url       = $iri->uri;		} 		// Massage the type to ensure we support it.		$type = strtoupper($type); 		if (!isset($options['data_format'])) {			if (in_array($type, [self::HEAD, self::GET, self::DELETE], true)) {				$options['data_format'] = 'query';			} else {				$options['data_format'] = 'body';			}		}	}

Changelog

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.

About this page

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