Opened 4 years ago
Closed 4 years ago
#47659 closed defect (bug) (invalid)
total_time not available through WordPress' HTTP API
Reported by: |
|
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.
Hi @flexithemes, welcome to WordPress Trac! Thanks for the ticket.
You can use the
requests-curl.after_request
hook to get access tototal_time
, where curl.after_request is a Requests library hook, and therequests-
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;