wp_get_http_headers()
Retrieves the HTTP headers of the specified URL.
No Hooks.
Returns
\WpOrg\Requests\Utility\CaseInsensitiveDictionary|false.
falseif headers could not be retrieved.-
Arrayof header data in case of successful retrieval:Array ( [server] => nginx/1.6.0 [date] => Sun, 02 Nov 2014 20:56:00 GMT [content-type] => text/html; charset=UTF-8 [connection] => close [vary] => Accept-Encoding [x-powered-by] => PHP/5.3.18 [x-pingback] => http://example.com/core/xmlrpc.php [expires] => Wed, 11 Jan 1984 05:00:00 GMT [cache-control] => no-cache, must-revalidate, max-age=0 [pragma] => no-cache )
Usage
wp_get_http_headers( $url );
- $url(string) (required)
- URL, whose HTTP headers should be retrieved.
Examples
#1 Get HTTP headers by URL
Sometimes it is enough to get HTTP headers instead of getting all the content of the specified URL. Getting a server response is usually 2-3 times faster:
$r = wp_get_http_headers( 'http://wordpress.org' ); print_r( $r );
this code will display such an array:
Array ( [server] => nginx [date] => Sun, 02 Nov 2014 20:39:35 GMT [content-type] => text/html [content-length] => 154 [connection] => close [location] => https://wordpress.org/ )
#2 Processing GET request
We can send a GET request by receiving headers only and it will be processed by remote server. For example, we need to trigger the following URL to run some task: http://example.com?foo=param:
wp_get_http_headers( 'http://example.com?foo=param' );
Changelog
| Since 1.5.1 | Introduced. |
wp_get_http_headers() wp get http headers code WP 6.8.3
function wp_get_http_headers( $url, $deprecated = false ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.7.0' );
}
$response = wp_safe_remote_head( $url );
if ( is_wp_error( $response ) ) {
return false;
}
return wp_remote_retrieve_headers( $response );
}