on_stats

摘要

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

类型

  • callable

常量

GuzzleHttp\RequestOptions::ON_STATS

该回调接收 GuzzleHttp\TransferStats 对象。

1. use GuzzleHttp\TransferStats;

3. $client = new GuzzleHttp\Client();

5. $client->request('GET', 'http://httpbin.org/stream/1024', [
6. 'on_stats' => function (TransferStats $stats) {
7. echo $stats->getEffectiveUri() . "\n";
8. echo $stats->getTransferTime() . "\n";
9. var_dump($stats->getHandlerStats());

11. // You must check if a response was received before using the
12. // response object.
13. if ($stats->hasResponse()) {
14. echo $stats->getResponse()->getStatusCode();
15. } else {
16. // Error data is handler specific. You will need to know what
17. // type of error data your handler uses before using this
18. // value.
19. var_dump($stats->getHandlerErrorData());
20. }
21. }
22. ]);