wppaste
WordPress

Curl::request( string|WpOrg\Requests\Transport\Stringable $url, array $headers = [], string|array $data = [], array $options = [] ): string

Source
wp-includes/Requests/src/Transport/Curl.php:149
Perform a request

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|WpOrg\Requests\Transport\Stringable
URL to request
$headersarrayoptional
Associative array of request headersDefault: []
$datastring|arrayoptional
Data to send either as the POST body, or as parameters in the URL for a GET/HEADDefault: []
$optionsarrayoptional
Request options, see \WpOrg\Requests\Requests::response() for documentationDefault: []

Return value

string
Raw HTTP result

Performance profile

How much work a call to Curl::request() 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
Heavy

Reads or writes the filesystem via fopen().

Scaling
Constant

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

Instructions
17–151

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

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

  • filesystemfilesystem accessfopen()called directly

What one call costs · 26 distinct outcomes

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

WhenInstructionsCalls it makes
always17–31::InputValidator(), ::InvalidArgument()
always58–64::InputValidator(), ->setup_handle(), fopen(), error_get_last(), ->dispatch()
always89–107::InputValidator(), ->setup_handle(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)98–114::InputValidator(), ->setup_handle(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
always99–117::InputValidator(), ->setup_handle(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
always101–119::InputValidator(), ->setup_handle(), fopen(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
always105–123::InputValidator(), ->setup_handle(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)105–120::InputValidator(), ->setup_handle(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)108–124::InputValidator(), ->setup_handle(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)110–126::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
always111–129::InputValidator(), ->setup_handle(), fopen(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)114–130::InputValidator(), ->setup_handle(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
14 further outcomes, up to 151 instructions
isset($options)114–123::InputValidator(), ->setup_handle(), curl_setopt(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)115–130::InputValidator(), ->setup_handle(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
always117–135::InputValidator(), ->setup_handle(), fopen(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)117–132::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)120–136::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)121–136::InputValidator(), ->setup_handle(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)124–133::InputValidator(), ->setup_handle(), curl_setopt(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)126–142::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)126–135::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)127–142::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)130–139::InputValidator(), ->setup_handle(), curl_setopt(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)133–148::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)136–145::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()
isset($options)142–151::InputValidator(), ->setup_handle(), fopen(), curl_setopt(), curl_setopt(), curl_setopt(), curl_exec(), ->dispatch(), curl_errno(), curl_errno(), curl_setopt(), curl_exec(), ->process_response(), curl_setopt(), curl_setopt()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev20217–151161 fewer instruction than PHP 8.5
8.520317–15116
8.420317–15116
8.320317–15116
8.220317–15116
8.120317–15116
7.420317–15116

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 · 5

  • \WpOrg\Requests\Utility\InputValidator::is_string_or_stringable()
  • \WpOrg\Requests\Exception\InvalidArgument::create()
  • \WpOrg\Requests\Transport\Curl::setup_handle()
  • \WpOrg\Requests\Exception::__construct()
  • \WpOrg\Requests\Transport\Curl::process_response()

Source code

	public function request($url, $headers = [], $data = [], $options = []) {		if (InputValidator::is_string_or_stringable($url) === false) {			throw InvalidArgument::create(1, '$url', 'string|Stringable', gettype($url));		} 		if (is_array($headers) === false) {			throw InvalidArgument::create(2, '$headers', 'array', gettype($headers));		} 		if (!is_array($data) && !is_string($data)) {			if ($data === null) {				$data = '';			} else {				throw InvalidArgument::create(3, '$data', 'array|string', gettype($data));			}		} 		if (is_array($options) === false) {			throw InvalidArgument::create(4, '$options', 'array', gettype($options));		} 		$this->hooks = $options['hooks']; 		$this->setup_handle($url, $headers, $data, $options); 		$options['hooks']->dispatch('curl.before_send', [&$this->handle]); 		if ($options['filename'] !== false) {			// phpcs:ignore WordPress.PHP.NoSilencedErrors -- Silenced the PHP native warning in favour of throwing an exception.			$this->stream_handle = @fopen($options['filename'], 'wb');			if ($this->stream_handle === false) {				$error = error_get_last();				throw new Exception($error['message'], 'fopen');			}		} 		$this->response_data       = '';		$this->response_bytes      = 0;		$this->response_byte_limit = false;		if ($options['max_bytes'] !== false) {			$this->response_byte_limit = $options['max_bytes'];		} 		if (isset($options['verify'])) {			if ($options['verify'] === false) {				curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);				curl_setopt($this->handle, CURLOPT_SSL_VERIFYPEER, 0);			} elseif (is_string($options['verify'])) {				curl_setopt($this->handle, CURLOPT_CAINFO, $options['verify']);			}		} 		if (isset($options['verifyname']) && $options['verifyname'] === false) {			curl_setopt($this->handle, CURLOPT_SSL_VERIFYHOST, 0);		} 		curl_exec($this->handle);		$response = $this->response_data; 		$options['hooks']->dispatch('curl.after_send', []); 		if (curl_errno($this->handle) === CURLE_WRITE_ERROR || curl_errno($this->handle) === CURLE_BAD_CONTENT_ENCODING) {			// Reset encoding and try again			curl_setopt($this->handle, CURLOPT_ENCODING, 'none'); 			$this->response_data  = '';			$this->response_bytes = 0;			curl_exec($this->handle);			$response = $this->response_data;		} 		$this->process_response($response, $options); 		// Need to remove the $this reference from the curl handle.		// Otherwise \WpOrg\Requests\Transport\Curl won't be garbage collected and the curl_close() will never be called.		curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null);		curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); 		return $this->headers;	}

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/Transport/Curl.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.