I'm trying to measure the response duration for some services called from my application.
I just need a simple method and a rough value is good enough, and I thought I could simply use total_time
from curl_getinfo
, and get this value from $response->getInfo()
, but it is always 0.
Is there some configuration or header that I am missing?
I use
if (!empty($headers)) {
$httpClient = HttpClient::create(['headers' => [
'Content-Type' => 'application/json',
'accept' => 'application/json',
]]);
} else {
$httpClient = HttpClient::create();
}
if ($method == 'GET') {
$response = $httpClient->request($method, $url . $pathParams);
} else {
$response = $httpClient->request($method, $url, ['body' => json_encode($arguments)]);
}
dd($response->getInfo());
and the output contains mostly empty values
^ array:44 [
"response_headers" => []
"http_code" => 0
"error" => null
"canceled" => false
"http_method" => "POST"
"user_data" => null
"start_time" => 1650918719.2348
"url" => "https://myworkingexample.com"
"content_type" => null
"header_size" => 0
"request_size" => 0
"filetime" => -1
"ssl_verify_result" => 0
"redirect_count" => 0
"total_time" => 0.0
"namelookup_time" => 0.0
"connect_time" => 0.0
"pretransfer_time" => 0.0
"size_upload" => 0.0
"size_download" => 0.0
"speed_download" => 0.0
"speed_upload" => 0.0
"download_content_length" => -1.0
"upload_content_length" => -1.0
"starttransfer_time" => 0.0
"redirect_time" => 0.0
"redirect_url" => null
"primary_ip" => ""
"certinfo" => []
"primary_port" => 0
"local_ip" => ""
"local_port" => 0
"http_version" => 0
"protocol" => 0
"ssl_verifyresult" => 0
"scheme" => ""
"appconnect_time_us" => 0
"connect_time_us" => 0
"namelookup_time_us" => 0
"pretransfer_time_us" => 0
"redirect_time_us" => 0
"starttransfer_time_us" => 0
"total_time_us" => 0
"debug" => ""
]
The HttpClient is "lazy", and will only execute/resolve the request when it's actually needed.
A call to
ResponseInterface::getInfo()
is defined to be non-blocking, and won't execute the request. It will just tell you the current state of the request. If you haven't actually executed it, the returned info will be empty, as you are getting.For
getInfo()
to return meaningful data, you'll need to call first one of the "blocking" methods. E.g.:$response->getContent()
$response->getStatusCode()
$response->getHeaders()
Any of these can throw an exception, so you may need to deal with that before you are able to call
getInfo()
.