Adapter of ObserverInterface from slepic/http-transfer package to guzzlehttp/guzzle middleware.
PHP >=5.6
Install with composer.
composer require slepic/guzzle-http-observing-middleware
Wrap any instance of \Slepic\Http\Transfer\Observer\ObserverInterface from package slepic/http-transfer in the \Slepic\Guzzle\Http\ObservingMiddleware\ObservingMiddleware and pass it to handler stack of your guzzle client.
All requests sent through the guzzle client will now be notified when requests are starting to get processed and when responses are received.
See an example where we use \Slepic\Http\Transfer\History\HistoryObserver to log requests and responses with timing.
$storage = new ArrayStorage();
$observer = new HistoryObserver($storage);
$middleware = new ObservingMiddleware($observer);
$client = new \GuzzleHttp\Client();
$client->getConfig('handler')->unshift($middleware);
try {
$response = $client->request($method, $uri);
} catch (\Exception $e) {
assert($storage[0]->getRequest()->getMethod() === $method);
assert((string)($storage[0]->getRequest()->getUri()) === $uri);
assert($storage[0]->getException() === $e);
assert(0 < ($storage[0]->getEndTime() - $storage[0]->getStartTime()));
throw $e;
}
assert($storage[0]->getRequest()->getMethod() === 'GET');
assert((string)($storage[0]->getRequest()->getUri()) === $uri);
assert($storage[0]->getResponse() === $response);
assert(0 < ($storage[0]->getEndTime() - $storage[0]->getStartTime()));
- See slepic/http-transfer-observer-implementation for known observers.
- See slepic/psr-http-message-tracy-panel to get your http client transfer into Tracy.