PHP code example of asjustas / guzzle-logger-middleware

1. Go to this page and download the library: Download asjustas/guzzle-logger-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asjustas / guzzle-logger-middleware example snippets




use GuzzleHttp\HandlerStack;
use AJ\Guzzle\Middleware\Logger;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;

$stack = HandlerStack::create();
$stack
    ->push(
        new Logger(
            function (RequestInterface $request, ?ResponseInterface $response, array $context) {
                print_r(
                    [
                        'uri' => (string)$request->getUri(),
                        'req' => (string)$request->getBody(),
                        'res' => $response ? (string)$response->getBody() : '',
                        'context' => $context,
                    ]
                );
            }
        )
    );

$client = new Client([
    'handler' => $stack,
]);

$client
    ->request(
        'POST',
        'http://example.com/404',
        [
            'body' => 'Hello World',
            'logger' => [
                'enabled' => true,
                'context' => [
                    'something' => 'important',
                ],
            ],
        ]
    );