wp_remote_retrieve_headers()WP 2.7.0

Retrieves all response header fields from the passed response object.

If you need to retrieve a single field, use wp_remote_retrieve_header()

See also an analog of this function: wp_get_http_headers()

No Hooks.

Returns

\WpOrg\Requests\Utility\CaseInsensitiveDictionary|Array. All response header fields. Empty array if an invalid parameter is passed. An example of what the function may return (depends on the request):

Array (
	[date] => Thu, 30 Sep 2010 15:16:36 GMT
	[server] => Apache
	[x-powered-by] => PHP/5.3.3
	[x-server] => 10.90.6.243
	[expires] => Thu, 30 Sep 2010 03:16:36 GMT
	[cache-control] => Array
		(
			[0] => no-store, no-cache, must-revalidate
			[1] => post-check=0, pre-check=0
		)

	[vary] => Accept-Encoding
	[content-length] => 1641
	[connection] => close
	[content-type] => application/php
)

Usage

wp_remote_retrieve_headers( $response );
$response(array) (required)
Response object obtained using one of the functions: wp_remote_get(), wp_remote_post(), wp_remote_head() or wp_remote_request().

Examples

0

#1 Get all the headers of the query response

$response = wp_remote_get( 'http://httpbin.org/get?a=b&c=d' );
$headers = wp_remote_retrieve_headers( $response );

print_r( $headers );

/* We get:
Requests_Utility_CaseInsensitiveDictionary Object
(
	[data:protected] => Array
		(
			[date] => Thu, 09 Jun 2022 02:27:42 GMT
			[content-type] => application/json
			[content-length] => 407
			[server] => gunicorn/19.9.0
			[access-control-allow-origin] => *
			[access-control-allow-credentials] => true
		)

)
*/

To access only one value, you can just type in the array key:

$headers = wp_remote_retrieve_headers( $response );

$headers['content-length']; // 407

Notes

Changelog

Since 2.7.0 Introduced.
Since 4.6.0 Return value changed from an array to an WpOrg\Requests\Utility\CaseInsensitiveDictionary instance.

wp_remote_retrieve_headers() code WP 6.9.1

function wp_remote_retrieve_headers( $response ) {
	if ( is_wp_error( $response ) || ! isset( $response['headers'] ) ) {
		return array();
	}

	return $response['headers'];
}