我试图使用Guzzle (v 6)分析来自PHP客户端的API服务器的请求。
在guese5.3中有这样的complete和before事件处理。
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中做到这一点呢?
发布于 2015-08-07 05:10:24
是用中间件发现的。这是密码。
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
]);https://stackoverflow.com/questions/31869708
复制相似问题