我正在尝试使用Guzzle(v 6)从PHP客户端分析对API服务器的请求.
在Guzzle 5.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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我怎么在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;
});
};
};
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样附加分析器。
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(Profiler::profile());
$client = new \GuzzleHttp\Client([
'handler' => $stack
]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
847 次 |
| 最近记录: |