wp_remote_retrieve_headers()WP 2.7.0

Retrieve only the headers from the raw response.

No Hooks.

Return

\WpOrg\Requests\Utility\CaseInsensitiveDictionary|Array. The headers of the response, or empty array if incorrect parameter given.

Usage

wp_remote_retrieve_headers( $response );
$response(array|WP_Error) (required)
HTTP response.

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

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

	return $response['headers'];
}