WP_Http::processHeaders( string|array $headers, string $url = '' ): array
- Since
- 2.7.0
- Source
wp-includes/class-wp-http.php:723
Compatibility
- WordPress
- since 2.7.0
- 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|array- The original headers. If a string is passed, it will be converted to an array. If an array is passed, then it is assumed to be raw header data with numeric keys with the headers as the values.
No headers must be passed that were already processed. $urlstringoptional- The URL that was requested. Default empty.Default:
''
Return value
array- Processed string headers. If duplicate headers are encountered, then a numbered array is returned as the value of that header-key.
$responsearray@type int $code The response status code. Default 0.$messagestringThe response message. Default empty. } @type array $newheaders The processed header data as a multidimensional array.$cookiesWP_Http_Cookie[]If the original headers contain the 'Set-Cookie' key, an array containingWP_Http_Cookieobjects is returned.
Performance profile
How much work a call to WP_Http::processHeaders() 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
- 23–46
- Plugin surface
- None
- Called by
- 3
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 108.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
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_Http::processHeaders() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($headers) && !$i | 23–24 | none |
!is_string($headers) && $i && !empty($headers[$i]) | 34–35 | array_splice() |
is_string($headers) && !$i | 34–35 | explode() |
is_string($headers) && $i && !empty($headers[$i]) | 45–46 | explode(), array_splice() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 108 | 23–46 | 11 | |
| 8.5 | 108 | 23–46 | 11 | |
| 8.4 | 108 | 23–46 | 11 | 14 fewer instructions than PHP 8.3 |
| 8.3 | 122 | 23–55 | 11 | |
| 8.2 | 122 | 23–55 | 11 | |
| 8.1 | 122 | 23–55 | 11 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 123 | 23–56 | 11 |
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 · 2
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- WP_Http_Cookie::__construct()Sets up this cookie object.
Used by · 3
- WP_Http::request()Send an HTTP request to a URI.
- WP_Http_Curl::request()Send a HTTP request to a URI using cURL extension.
- WP_Http_Streams::request()Send a HTTP request to a URI using PHP Streams.
Source code
public static function processHeaders( $headers, $url = '' ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid // Split headers, one per array element. if ( is_string( $headers ) ) { // Tolerate line terminator: CRLF = LF (RFC 2616 19.3). $headers = str_replace( "\r\n", "\n", $headers ); /* * Unfold folded header fields. LWS = [CRLF] 1*( SP | HT ) <US-ASCII SP, space (32)>, * <US-ASCII HT, horizontal-tab (9)> (RFC 2616 2.2). */ $headers = preg_replace( '/\n[ \t]/', ' ', $headers ); // Create the headers array. $headers = explode( "\n", $headers ); } $response = array( 'code' => 0, 'message' => '', ); /* * If a redirection has taken place, The headers for each page request may have been passed. * In this case, determine the final HTTP header and parse from there. */ for ( $i = count( $headers ) - 1; $i >= 0; $i-- ) { if ( ! empty( $headers[ $i ] ) && ! str_contains( $headers[ $i ], ':' ) ) { $headers = array_splice( $headers, $i ); break; } } $cookies = array(); $newheaders = array(); foreach ( (array) $headers as $tempheader ) { if ( empty( $tempheader ) ) { continue; } if ( ! str_contains( $tempheader, ':' ) ) { $stack = explode( ' ', $tempheader, 3 ); $stack[] = ''; list( , $response['code'], $response['message']) = $stack; continue; } list($key, $value) = explode( ':', $tempheader, 2 ); $key = strtolower( $key ); $value = trim( $value ); if ( isset( $newheaders[ $key ] ) ) { if ( ! is_array( $newheaders[ $key ] ) ) { $newheaders[ $key ] = array( $newheaders[ $key ] ); } $newheaders[ $key ][] = $value; } else { $newheaders[ $key ] = $value; } if ( 'set-cookie' === $key ) { $cookies[] = new WP_Http_Cookie( $value, $url ); } } // Cast the Response Code to an int. $response['code'] = (int) $response['code']; return array( 'response' => $response, 'headers' => $newheaders, 'cookies' => $cookies, ); }Changelog
Introduced in 2.7.0. 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.0.4 tag, from
src/wp-includes/class-wp-http.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.