Guzzle 6 - Get request total time
Asked Answered
I

5

11

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.

Intimate answered 10/7, 2015 at 12:50 Comment(0)
N
24

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

Nippon answered 18/9, 2015 at 9:32 Comment(0)
A
4

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());
Albion answered 31/7, 2020 at 4:8 Comment(0)
S
2

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(); 
    }
]);
Salema answered 10/9, 2019 at 9:34 Comment(0)
M
0
$client = new GuzzleHttp\Client();
$one = microtime(1);
$response = $client->get('http://www.google.com/');
$two = microtime(1);
echo 'Total Request time: '. ( $two - $one );
Mosstrooper answered 10/7, 2015 at 12:55 Comment(2)
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.Primate
You need microtime(true) if you want to do calculations with the return value.Frederique
H
-1

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.

Hackle answered 1/9, 2015 at 9:50 Comment(3)
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.Beau
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.Dildo
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.Hackle

© 2022 - 2025 — McMap. All rights reserved.