wppaste
WordPress

WP_Http::processHeaders( string|array $headers, string $url = '' ): array

Since
2.7.0
Source
wp-includes/class-wp-http.php:722
Transforms header string into an array.

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.
    • $messagestring

      The 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 containing WP_Http_Cookie objects 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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
23–46

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

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.

WhenInstructionsCalls it makes
!is_string($headers) && !$i23–24none
!is_string($headers) && $i && !empty($headers[$i])34–35array_splice()
is_string($headers) && !$i34–35explode()
is_string($headers) && $i && !empty($headers[$i])45–46explode(), array_splice()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10823–4611
8.510823–4611
8.410823–461114 fewer instructions than PHP 8.3
8.312223–5511
8.212223–5511
8.112223–55111 fewer instruction than PHP 7.4
7.412323–5611

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

Used by · 3

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.

  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 6.9.7 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.