on_stats

摘要

on_stats 允许你获取请求传输数据统计以及处理器在底层传输的详情. on_stats 是个回调,当处理器完成传输一个请求的时候被调用。 该回调被调用请求传输数据统计、接收到响应,或遇到错误,包含发送请求数据时间的总量。

类型

  • callable

常量

GuzzleHttp\RequestOptions::ON_STATS

该回调接收 GuzzleHttp\TransferStats 对象。

  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. ]);