Guzzle 6 - Get request total time

天涯浪子 提交于 2019-12-08 16:27:41

问题


I'm searching to retrieve the request total time in Guzzle 6, just after a simple GET request :

$client = new GuzzleHttp\Client();
$response = client->get('http://www.google.com/');

But can't find anything in the docs about that. Any idea ?

Thanks a lot.


回答1:


In Guzzle 6.1.0 You can use the 'on_stats' request option to get transfer time etc.

More information can be found at Request Options - on_stats

https://github.com/guzzle/guzzle/releases/tag/6.1.0




回答2:


$client = new GuzzleHttp\Client();
$one = microtime(1);
$response = $client->get('http://www.google.com/');
$two = microtime(1);
echo 'Total Request time: '. ( $two - $one );



回答3:


An specific example based on the @Michael post.

$client = new GuzzleHttp\Client();

$response = $client->get('http://www.google.com/', [
    'on_stats' => function (\GuzzleHttp\TransferStats $stats) {
       echo $stats->getEffectiveUri() . ' : ' . $stats->getTransferTime(); 
    }
]);



回答4:


I had a similar problem although it's still Guzzle 5.3.

See Guzzle 5.3 - Get request duration for asynchronous requests

Maybe listening to an event in Guzzle6 and retrieving the TransferInfo will do the trick for you too.

This works for synchronous and asynchronous requests alike.



来源:https://stackoverflow.com/questions/31341254/guzzle-6-get-request-total-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!