Skip to content

Instantly share code, notes, and snippets.

@never615
Last active January 12, 2018 03:36
Show Gist options
  • Save never615/b018db2c693eaa5e644b8c30bb83de7b to your computer and use it in GitHub Desktop.
Save never615/b018db2c693eaa5e644b8c30bb83de7b to your computer and use it in GitHub Desktop.
guzzle middleware tap
$middleware = Middleware::tap(function (RequestInterface $request, $options) use($log) {
$log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null;
$log->payload = $request->getBody()->getContents();
}, function ($request, $options, Promise $response) use($log) {
$response->then(function (ResponseInterface $response) use($log) {
$log->status = $response->getStatusCode();
$log->response = $response->getBody()->getContents();
$log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null;
$log->save();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment