Guzzle 6 - Get request total time

14,243

Solution 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

Solution 2

You can use setter and getter.

   private $totaltime = 0;

   public function getTotaltime(){
        return $this->totaltime;

    }
    public function setTotaltime($time){
        $this->totaltime = $time;

    }

    $reqtime= new self();
    $response = $client->post($endpointLogin, [
                    'json' => $payload,
                    'headers' => $this->header,
                    'on_stats' => function (TransferStats $stats) use ($reqtime)  {

                      $stats->getTransferTime();

                      //** set it here **//
                      $reqtime->setTotaltime($stats->getTransferTime());

                }

      ]);

       dd($reqtime->getTotaltime());

Solution 3

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

Solution 4

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(); 
    }
]);
Share:
14,243
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

  • xpy
    xpy over 8 years
    if it does work for you for one request, just use an array to store many of them, like: $time['start']['google'] = microtime(); store the end at $time['end']['google'] and iterate the whenever you want to.
  • Shaun Bramley
    Shaun Bramley over 8 years
    Guzzle 6 does not make use of events. It uses promises. Guzzle 6.1 added native support for providing access to transaction statistics through the use of providing a callable to the "on_stats" request option.
  • mrDinkelman
    mrDinkelman about 7 years
    Your answer contains wrong Guzzle version. Topic starter asked about Guzzle 6+. You provide answer for 5.3. These versions of Guzzle are incompatible to each other.
  • Worp
    Worp about 7 years
    While what you're saying is true, as I have already stated at the very beginning of my answer, there is no problem with it. While this was just a suggestions to see if a similar concept still exists in Guzzle 6+ and, since it seems not, doesn't help the TS, someone else coming by with the description of the problem but on version 5.x will find this useful. It's hard enough to find valid info about guzzle as it is. Also: This answer is 1,5 years old. Let's not raise the dead.
  • vishalknishad
    vishalknishad over 5 years
    You need microtime(true) if you want to do calculations with the return value.