Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 216
Benoth Avatar asked Jul 10 '15 12:07

Benoth


2 Answers

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

like image 116
Michael Avatar answered Nov 06 '22 13:11

Michael


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());
like image 35
CHHUM Sina Avatar answered Nov 06 '22 11:11

CHHUM Sina