Get remote URL HTTP headers with wp_remote_retrieve_header in WordPress

The wp_remote_retrieve_header function is a part of WordPress’s HTTP API. This function is designed to extract a specific header from the array of headers returned by the HTTP request.

When a HTTP request is made, the server responds with a set of headers. These headers provide important information about the response, such as its content type, length, encoding, status, and other metadata. The wp_remote_retrieve_header function allows developers to retrieve a specific header from this set.

It can be useful in various scenarios where the information from the server’s response headers is required. For instance, it can help in determining the type of content returned by the server, checking the status of the request, or understanding the server’s caching policies.

It’s important to note that this function only retrieves a single header at a time. If multiple headers need to be accessed, the function must be called multiple times, each time with a different header name.

Parameters Accepted by the wp_remote_retrieve_header Function

The wp_remote_retrieve_header function in WordPress accepts two parameters:

  • $response (array|WP_Error): This is a mandatory parameter. It represents the HTTP response.
  • $header (string): This is also a required parameter. It signifies the name of the header from which the value needs to be retrieved.

Return Value of the wp_remote_retrieve_header Function

The wp_remote_retrieve_header function returns either an array or a string, depending on the situation. If multiple headers with identical names are retrieved, the function will return an array. However, if an incorrect parameter is given or if the header does not exist, the function will return an empty string.

Examples

How to Retrieve a Specific Header from a Remote GET Request

The following code snippet sends a GET request to a specified URL and retrieves the ‘Content-Type’ header from the response.

$url = 'http://example.com';
$response = wp_remote_get( $url );
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
echo '<p>Content Type: '. $content_type .'</p>';

How to Check if a Specific Header Exists in a Remote GET Request

This code snippet sends a GET request to a specified URL and checks if a ‘Location’ header exists in the response.

$url = 'http://example.com';
$response = wp_remote_get( $url );
$location_header = wp_remote_retrieve_header( $response, 'location' );
if ( ! empty( $location_header ) ) {
 echo '<p>Location Header Exists: '. $location_header .'</p>';
} else {
 echo '<p>Location Header Does Not Exist</p>';
}

How to Retrieve All Headers from a Remote GET Request

The following code snippet sends a GET request to a specified URL and retrieves all the headers from the response.

$url = 'http://example.com';
$response = wp_remote_get( $url );
$headers = wp_remote_retrieve_headers( $response );
foreach ( $headers as $name => $value ) {
 echo '<p>'. $name .': '. $value .'</p>';
}

In the above code, $url is the URL where the GET request is sent. $response is the response received from the GET request. $headers is an array containing all the headers in the response. The foreach loop iterates over each header in the $headers array and displays the name and value of each header.

Conclusion

The wp_remote_retrieve_header function in WordPress is a tool that allows developers to extract specific header values from HTTP responses. It is a part of the HTTP API in WordPress and is typically used in conjunction with other functions like wp_remote_get or wp_remote_post, which send HTTP requests. The retrieved header values can be utilized for various purposes, such as debugging, handling redirects, or managing content types.

Related WordPress Functions