Fsockopen::request( string|WpOrg\Requests\Transport\Stringable $url, array $headers = [], string|array $data = [], array $options = [] ): string
- Source
wp-includes/Requests/src/Transport/Fsockopen.php:77
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 Fsockopen::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
- Scaling
- Scales with input
- Instructions
- 163–229
- Plugin surface
- None
- Called by
- 0
Reads or writes the filesystem via fwrite().
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 510.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- filesystemfilesystem access
fwrite()called directly
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost Fsockopen::request() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && !$options | 163–174 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), fclose() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && !feof() | 197–211 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), feof(), stream_get_meta_data(), ->dispatch() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && $options && $download === false | 200–214 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), fopen(), error_get_last(), ->dispatch() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && feof() | 204–218 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), feof(), fclose() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && feof() | 205–219 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), feof(), fclose(), fclose() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && $options && $download !== false && !feof() | 207–221 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), fopen(), feof(), stream_get_meta_data(), ->dispatch() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && $options && $download !== false && feof() | 214–228 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), fopen(), feof(), fclose() |
is_array($data) && !empty($url_parts) && $data_format !== "query" && isset($case_insensitive_headers) && $options && $download !== false && feof() | 215–229 | ::InputValidator(), ->dispatch(), parse_url(), stream_context_create(), ceil(), stream_socket_client(), restore_error_handler(), ::format_get(), sprintf(), ->accept_encoding(), ::Requests(), fwrite(), floor(), stream_set_timeout(), stream_get_meta_data(), fopen(), feof(), fclose(), fclose() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 509 | 163–229 | 54 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 510 | 163–229 | 54 | |
| 8.4 | 510 | 163–229 | 54 | 17 fewer instructions than PHP 8.3 |
| 8.3 | 527 | 166–236 | 54 | 10 fewer instructions than PHP 8.2 |
| 8.2 | 537 | 166–236 | 55 | |
| 8.1 | 537 | 166–236 | 55 | |
| 7.4 | 537 | 166–236 | 55 |
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 · 8
- \WpOrg\Requests\Utility\InputValidator::is_string_or_stringable()
- \WpOrg\Requests\Exception\InvalidArgument::create()
- \WpOrg\Requests\Exception::__construct()
- \WpOrg\Requests\Utility\CaseInsensitiveDictionary::__construct()
- \WpOrg\Requests\Transport\Fsockopen::verify_certificate_from_context()
- \WpOrg\Requests\Transport\Fsockopen::format_get()
- \WpOrg\Requests\Transport\Fsockopen::accept_encoding()
- \WpOrg\Requests\Requests::flatten()
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)); } $options['hooks']->dispatch('fsockopen.before_request'); $url_parts = parse_url($url); if (empty($url_parts)) { throw new Exception('Invalid URL.', 'invalidurl', $url); } $host = $url_parts['host']; $context = stream_context_create(); $verifyname = false; $case_insensitive_headers = new CaseInsensitiveDictionary($headers); // HTTPS support if (isset($url_parts['scheme']) && strtolower($url_parts['scheme']) === 'https') { $remote_socket = 'ssl://' . $host; if (!isset($url_parts['port'])) { $url_parts['port'] = Port::HTTPS; } $context_options = [ 'verify_peer' => true, 'capture_peer_cert' => true, ]; $verifyname = true; // SNI, if enabled (OpenSSL >=0.9.8j) // phpcs:ignore PHPCompatibility.Constants.NewConstants.openssl_tlsext_server_nameFound if (defined('OPENSSL_TLSEXT_SERVER_NAME') && OPENSSL_TLSEXT_SERVER_NAME) { $context_options['SNI_enabled'] = true; if (isset($options['verifyname']) && $options['verifyname'] === false) { $context_options['SNI_enabled'] = false; } } if (isset($options['verify'])) { if ($options['verify'] === false) { $context_options['verify_peer'] = false; $context_options['verify_peer_name'] = false; $verifyname = false; } elseif (is_string($options['verify'])) { $context_options['cafile'] = $options['verify']; } } if (isset($options['verifyname']) && $options['verifyname'] === false) { $context_options['verify_peer_name'] = false; $verifyname = false; } // Handle the PHP 8.4 deprecation (PHP 9.0 removal) of the function signature we use for stream_context_set_option(). // Ref: https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#stream_context_set_option if (function_exists('stream_context_set_options')) { // PHP 8.3+. // phpcs:ignore PHPCompatibility.FunctionUse.NewFunctions.stream_context_set_optionsFound stream_context_set_options($context, ['ssl' => $context_options]); } else { // PHP < 8.3. // phpcs:ignore PHPCompatibility.FunctionUse.OptionalToRequiredFunctionParameters stream_context_set_option($context, ['ssl' => $context_options]);Changelog
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/Requests/src/Transport/Fsockopen.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.