Skip to content

Instantly share code, notes, and snippets.

@Radon8472
Last active February 4, 2020 07:28
Show Gist options
  • Save Radon8472/5c9b6bccedd0239fefca64cd59421f01 to your computer and use it in GitHub Desktop.
Save Radon8472/5c9b6bccedd0239fefca64cd59421f01 to your computer and use it in GitHub Desktop.
Example for Debug-Outputs in GuzzleHttp version 6+
<?php
/**
* @see: https://github.com/guzzle/guzzle/blob/master/UPGRADING.md#migrating-to-middleware
* @see: http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html
**/
$handler = \GuzzleHttp\HandlerStack::create();
// watch request
$handler->push(\GuzzleHttp\Middleware::mapRequest(function (\psr\Http\Message\RequestInterface $request) {
printf("Request: %-8s %s\nHeaders-%s",$request->getMethod(),$request->getUri(),
print_r(array_map(function($x) {return $x[0];}, $request->getHeaders()), true)
);
return $request;
}));
// watch response
$handler->push(\GuzzleHttp\Middleware::mapResponse(function (\psr\Http\Message\ResponseInterface $response) {
printf("Response: %3d %s\nHeaders-%s", $response->getStatusCode(), $response->getReasonPhrase(),
print_r(array_map(function($x) {return $x[0];}, $response->getHeaders()), true)
);
return $response;
}));
$httpClient = new \GuzzleHttp\Client(array('handler' => $handler));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment