Benoth
Benoth

Reputation: 111

Guzzle 6 - Get request total time

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.

Upvotes: 11

Views: 20091

Answers (5)

CHHUM Sina
CHHUM Sina

Reputation: 421

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());

Upvotes: 4

Juan Lago
Juan Lago

Reputation: 1048

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(); 
    }
]);

Upvotes: 2

Mahdyfo
Mahdyfo

Reputation: 1173

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

Upvotes: 0

Michael
Michael

Reputation: 585

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

Upvotes: 24

Worp
Worp

Reputation: 1018

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.

Upvotes: -1

Related Questions