on_stats

Summary

on_stats allows you to get access to transfer statistics of a request and access the lower level transfer details of the handler associated with your client. on_stats is a callable that is invoked when a handler has finished sending a request. The callback is invoked with transfer statistics about the request, the response received, or the error encountered. Included in the data is the total amount of time taken to send the request.

Types

  • callable

Constant

GuzzleHttp\RequestOptions::ON_STATS

The callable accepts a GuzzleHttp\TransferStats object.

  1. use GuzzleHttp\TransferStats;
  2. $client = new GuzzleHttp\Client();
  3. $client->request('GET', 'http://httpbin.org/stream/1024', [
  4. 'on_stats' => function (TransferStats $stats) {
  5. echo $stats->getEffectiveUri() . "\n";
  6. echo $stats->getTransferTime() . "\n";
  7. var_dump($stats->getHandlerStats());
  8. // You must check if a response was received before using the
  9. // response object.
  10. if ($stats->hasResponse()) {
  11. echo $stats->getResponse()->getStatusCode();
  12. } else {
  13. // Error data is handler specific. You will need to know what
  14. // type of error data your handler uses before using this
  15. // value.
  16. var_dump($stats->getHandlerErrorData());
  17. }
  18. }
  19. ]);