Make WordPress Core

Opened 8 years ago

Closed 5 months ago

Last modified 5 weeks ago

#44174 closed defect (bug) (invalid)

wp_get_http_headers does not return status code or follow redirects

Reported by: tlhackque's profile tlhackque 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.

Change History (2)

#1 @pmbaldha
5 months ago

  • Resolution set to invalid
  • Status changed from new to closed

We can get the response code of the remote request by following the code below:

<?php
$args = [
                'timeout' => 1,
                'sslverify' => false,
        ];
$response = wp_remote_get( 'https://prashantwp.com/', $args );
$response_code = wp_remote_retrieve_response_code( $response );

The wp_get_http_headers() function is not intended to get the response code, so I am marking this ticket as invalid.

#2 @swissspidy
5 weeks ago

  • Milestone Awaiting Review deleted

Removing milestone from closed ticket.

Note: See TracTickets for help on using tickets.