如何分析 Guzzle 6 请求?

How do I profile Guzzle 6 requests?

我正在尝试使用 Guzzle (v 6) 分析从 PHP 客户端向 API 服务器发出的请求。

在 Guzzle 5.3 中有此 completebefore 事件处理。

class GuzzleProfiler implements SubscriberInterface
{
    public function getEvents()
    {
        return [
            'before'   => ['onBefore'],
            'complete' => ['onComplete']
        ];
    }

    public function onBefore(BeforeEvent $event, $name)
    {
         start_profiling();
    }

    public function onComplete(CompleteEvent $event, $name)
    {
         end_profiling();
    }
}

但是我如何在 v6 中执行此操作?

刚刚使用中间件找到它。这是代码。

class Profiler {

    /**
     * @return callable
     */
    public static function profile() {
        return function(callable $handler) {
            return function(\Psr\Http\Message\RequestInterface $request, array $options) use ($handler) {
                start_profiling();
                return $handler($request, $options)->then(function(\Psr\Http\Message\ResponseInterface $response) use ($token) {
                    end_profiling();
                    return $response;
                });
            };
        };
    }
}

然后像这样附加分析器。

$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(Profiler::profile());
$client = new \GuzzleHttp\Client([
   'handler' => $stack
]);