WordPress.org

Make WordPress Core

#47659 closed defect (bug) (invalid)

total_time not available through WordPress' HTTP API

Reported by: flexithemes Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.2.2
Component: HTTP API Keywords:
Focuses: Cc:

Description

I just submitted our SEO Checklist plugin and as part of the process the WordPress Plugin directory team requires the following:

WordPress comes with an extensive HTTP API that should be used instead of creating your own curl calls. It’s both faster and more extensive. It’ll fall back to curl if it has to, but it’ll use a lot of WordPress’ native functionality first.

However, the HTTP API doesn't appear to work for getting total_time, since total_time isn't in the header response.

I tried getting it a variety of different ways with the HTTP API, but couldn't. I also looked through the documentation and searched online, but there's nothing about total_time using the HTTP API. One other developer tried it too and couldn't figure it out.

Here's how I was doing it in CURL, which worked:

        $ch = curl_init(site_url());
        
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        
	if(curl_exec($ch)) {
	    $info = curl_getinfo($ch);
	    $response_time = $info['total_time'];
	}
        
	curl_close($ch);

Here's one example of what doesn't work:

        $result = wp_remote_get( site_url() );
        if ( is_array( $result ) ) {
            $headers = $result['headers']; // array of http header lines
        }
        $response_time = $headers['total_time'];

The above code is in the plugin in includes/class-seo-checks.php at line 100. Because it doesn't work, the response time of the server isn't currently measured during its check.

Here's PHP's documentation on curl_getinfo(), which includes total_time as an opt.

Here's Curl's official documentation on total_time.

Change History (3)

#1 @TimothyBlynJacobs
17 months ago

  • Focuses rest-api removed

#2 @SergeyBiryukov
17 months ago

  • Keywords close added

Hi @flexithemes, welcome to WordPress Trac! Thanks for the ticket.

You can use the requests-curl.after_request hook to get access to total_time (where curl.after_request is a Requests library hook, and the requests- prefix is added in WP_HTTP_Requests_Hooks::dispatch() when mapping it to a native WordPress action.

Here's an example that works for me;

class WP47659_Requests_Debug {

	var $total_time;

	function __construct() {
		add_action( 'plugins_loaded', array( $this, 'perform_request' ) );
		add_action( 'requests-curl.after_request', array( $this, 'store_total_time' ), 10, 2 );
	}

	function perform_request() {
		wp_remote_get( 'https://www.google.com/' );
		echo $this->total_time;
		die();
	}

	function store_total_time( $headers, $info ) {
		$this->total_time = $info['total_time'];
	}

}

new WP47659_Requests_Debug;
Version 0, edited 17 months ago by SergeyBiryukov (next)

#3 @pento
17 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.