【问题标题】:How do I profile Guzzle 6 requests?如何分析 Guzzle 6 请求?
【发布时间】:2015-10-30 10:11:59
【问题描述】:

我正在尝试使用 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 中做到这一点?

【问题讨论】:

标签: php guzzle6


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多