Last active
February 4, 2020 07:28
-
-
Save Radon8472/5c9b6bccedd0239fefca64cd59421f01 to your computer and use it in GitHub Desktop.
Example for Debug-Outputs in GuzzleHttp version 6+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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