#44174 closed defect (bug) (invalid)
wp_get_http_headers does not return status code or follow redirects
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.9.6 |
| Component: | HTTP API | Keywords: | |
| Focuses: | Cc: |
Description
wp_get_http_headers performs an HTTP HEAD request and returns a simple success or failure code. This function is listed as part of the "HTTP API" on https://codex.wordpress.org/Function_Reference.
If the target resource issues a redirect, it is not followed, nor is the specific redirect code returned. It is possible to check for the Location header & follow the redirect manually. But wp_get_http_headers should, at least optionally, process redirects. It should also return the HTTP status code and message. One way to do this would be the Apache model - provide a Status pseudo-header in the response (e.g. 'Status' => '200 OK')
If a WordPress resource returns a 404 code, the result is 'success' with an HTML page suitable for humans - HEAD, of course, simply says content-type: text/html. This is indistinguishable from 200 for the page.
The underlying wp_safe_remote_head API seems to return the status, but it's not returned from wp_get_http_headers. Note that wp_safe_remote_head is not listed on the function reference page, so I assume is not intended for public use.
(If context matters, I'm using it to implement a shortcode in a plugin.)
I'm leaving this as a "bug" since failure to provide status prevents a caller from detecting errors and correctly processing a response.
We can get the response code of the remote request by following the code below:
The wp_get_http_headers() function is not intended to get the response code, so I am marking this ticket as invalid.