Requests::parse_response( string $headers, string $url, array $req_headers, array $req_data, array $options ): WpOrg\Requests\Response
- Source
wp-includes/Requests/src/Requests.php:723
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
$headersstring- Full response text including headers and body
$urlstring- Original request URL
$req_headersarray- Original $headers array passed to request(), in case we need to follow redirects
$req_dataarray- Original $data array passed to request(), in case we need to follow redirects
$optionsarray- Original $options array passed to request(), in case we need to follow redirects
Return value
WpOrg\Requests\Response
Performance profile
How much work a call to Requests::parse_response() 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
- Light
- Scaling
- Scales with input
- Instructions
- 11–170
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 224.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 14 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost Requests::parse_response() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$options | 11–27 | none |
$options && empty($matches) | 46–60 | explode(), array_shift(), preg_match() |
$options && !empty($matches) && !isset($value) | 87–123 | explode(), array_shift(), preg_match(), ->is_redirect() |
$options && !empty($matches) | 94–130 | explode(), array_shift(), preg_match(), ::decompress(), ->is_redirect() |
$options && !empty($matches) | 96–132 | explode(), array_shift(), preg_match(), ::decode_chunked(), ->is_redirect() |
$options && !empty($matches) && isset($value) | 103–139 | explode(), array_shift(), preg_match(), ::decode_chunked(), ::decompress(), ->is_redirect() |
$options && !empty($matches) && ->is_redirect() | 119–147 | explode(), array_shift(), preg_match(), ->is_redirect(), ->dispatch(), ::request() |
$options && !empty($matches) && ->is_redirect() | 126–154 | explode(), array_shift(), preg_match(), ::decompress(), ->is_redirect(), ->dispatch(), ::request() |
$options && !empty($matches) && ->is_redirect() | 128–156 | explode(), array_shift(), preg_match(), ::decode_chunked(), ->is_redirect(), ->dispatch(), ::request() |
$options && !empty($matches) && ->is_redirect() | 129–154 | explode(), array_shift(), preg_match(), ->is_redirect(), ::Iri(), ->dispatch(), ::request() |
$options && !empty($matches) && isset($value) && ->is_redirect() | 135–163 | explode(), array_shift(), preg_match(), ::decode_chunked(), ::decompress(), ->is_redirect(), ->dispatch(), ::request() |
$options && !empty($matches) && ->is_redirect() | 136–161 | explode(), array_shift(), preg_match(), ::decompress(), ->is_redirect(), ::Iri(), ->dispatch(), ::request() |
2 further outcomes, up to 170 instructions
$options && !empty($matches) && ->is_redirect() | 138–163 | explode(), array_shift(), preg_match(), ::decode_chunked(), ->is_redirect(), ::Iri(), ->dispatch(), ::request() |
$options && !empty($matches) && isset($value) && ->is_redirect() | 145–170 | explode(), array_shift(), preg_match(), ::decode_chunked(), ::decompress(), ->is_redirect(), ::Iri(), ->dispatch(), ::request() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 224 | 11–170 | 20 | |
| 8.5 | 224 | 11–170 | 20 | |
| 8.4 | 224 | 11–170 | 20 | 26 fewer instructions than PHP 8.3 |
| 8.3 | 250 | 11–192 | 20 | |
| 8.2 | 250 | 11–192 | 20 | |
| 8.1 | 250 | 11–192 | 20 | |
| 7.4 | 250 | 11–192 | 20 |
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
- \WpOrg\Requests\Response::__construct()
- \WpOrg\Requests\Exception::__construct()
- \WpOrg\Requests\Requests::decode_chunked()
- \WpOrg\Requests\Requests::decompress()
- \WpOrg\Requests\Iri::absolutize()
- \WpOrg\Requests\Requests::request()
Source code
protected static function parse_response($headers, $url, $req_headers, $req_data, $options) { $return = new Response(); if (!$options['blocking']) { return $return; } $return->raw = $headers; $return->url = (string) $url; $return->body = ''; if (!$options['filename']) { $pos = strpos($headers, "\r\n\r\n"); if ($pos === false) { // Crap! throw new Exception('Missing header/body separator', 'requests.no_crlf_separator'); } $headers = substr($return->raw, 0, $pos); // Headers will always be separated from the body by two new lines - `\n\r\n\r`. $body = substr($return->raw, $pos + 4); if (!empty($body)) { $return->body = $body; } } // Pretend CRLF = LF for compatibility (RFC 2616, section 19.3) $headers = str_replace("\r\n", "\n", $headers); // Unfold headers (replace [CRLF] 1*( SP | HT ) with SP) as per RFC 2616 (section 2.2) $headers = preg_replace('/\n[ \t]/', ' ', $headers); $headers = explode("\n", $headers); preg_match('#^HTTP/(1\.\d)[ \t]+(\d+)#i', array_shift($headers), $matches); if (empty($matches)) { throw new Exception('Response could not be parsed', 'noversion', $headers); } $return->protocol_version = (float) $matches[1]; $return->status_code = (int) $matches[2]; if ($return->status_code >= 200 && $return->status_code < 300) { $return->success = true; } foreach ($headers as $header) { list($key, $value) = explode(':', $header, 2); $value = trim($value); preg_replace('#(\s+)#i', ' ', $value); $return->headers[$key] = $value; } if (isset($return->headers['transfer-encoding'])) { $return->body = self::decode_chunked($return->body); unset($return->headers['transfer-encoding']); } if (isset($return->headers['content-encoding'])) { $return->body = self::decompress($return->body); } //fsockopen and cURL compatibility if (isset($return->headers['connection'])) { unset($return->headers['connection']); } $options['hooks']->dispatch('requests.before_redirect_check', [&$return, $req_headers, $req_data, $options]); if ($return->is_redirect() && $options['follow_redirects'] === true) { if (isset($return->headers['location']) && $options['redirected'] < $options['redirects']) { if ($return->status_code === 303) { $options['type'] = self::GET; } $options['redirected']++; $location = $return->headers['location']; if (strpos($location, 'http://') !== 0 && strpos($location, 'https://') !== 0) { // relative redirect, for compatibility make it absolute $location = Iri::absolutize($url, $location); $location = $location->uri; } $hook_args = [ &$location,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 6.9.7 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.