wp_remote_retrieve_headers()WP 2.7.0

Получает все поля заголовка ответа из переданного объекта ответа.

Если нужно получить отдельное поле, используйте wp_remote_retrieve_header()

Смотрите также аналог этой функции: wp_get_http_headers()

Основа для: wp_get_http_headers()

Хуков нет.

Возвращает

\WpOrg\Requests\Utility\CaseInsensitiveDictionary|Массив. Все поля заголовка ответа. Пустой массив, если передан неверный параметр. Пример того, что может вернуть функция (зависит от запроса):

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
)

Использование

wp_remote_retrieve_headers( $response );
$response(массив) (обязательный)
Объект ответа, полученный с помощью одной из функций: wp_remote_get(), wp_remote_post(), wp_remote_head() или wp_remote_request().

Примеры

0

#1 Получим все заголовки ответа запроса

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

print_r( $headers );

/* Получим:
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
		)

)
*/

Чтобы получить доступ только к одному значению, можно просто ввести ключ массива:

$headers = wp_remote_retrieve_headers( $response );

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

Заметки

Список изменений

С версии 2.7.0 Введена.
С версии 4.6.0 Return value changed from an array to an WpOrg\Requests\Utility\CaseInsensitiveDictionary instance.

Код wp_remote_retrieve_headers() WP 6.4.3

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

	return $response['headers'];
}