PHP code example of exeque / guzzle-spy

1. Go to this page and download the library: Download exeque/guzzle-spy 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/ */

    

exeque / guzzle-spy example snippets


use ExeQue\Guzzle\Spy\Middleware;
use ExeQue\Guzzle\Spy\Spy;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

$middleware = new Middleware(
    $spy = new Spy(
        fn(
            string $id, 
            RequestInterface $request, 
            array $options,
        ) =>  ..., // Before the request is sent
        fn(
            string $id, 
            ResponseInterface $response, 
            RequestInterface $request, 
            array $options,
        ) => ..., // After the response is received
    ),
);
// or
$middleware = Spy::middleware(
    fn(
        string $id, 
        RequestInterface $request, 
        array $options,
    ) =>  ..., // Before the request is sent
    fn(
        string $id, 
        ResponseInterface $response, 
        RequestInterface $request, 
        array $options,
    ) => ..., // After the response is received
)

$handler = HandlerStack::create();

// It's recommended to use the spy
// as the last middleware in the stack.
// It will work regardless of where it is placed, 
// but any other middleware that modifies the request or response
// will affect the data that is reported to the spy.
$handler->push($middleware, 'spy');

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


use ExeQue\Guzzle\Spy\Contracts\Spy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class TimeLoggingSpy implements Spy
{
    private array $timers = [];

    public function __construct(
        private \Psr\Log\LoggerInterface $logger,
    ) {}

    public function before(string $id, RequestInterface $request, array $options): void {
        $this->timers[$id] = microtime(true);
    }
    
    public function after(string $id, ResponseInterface $response, RequestInterface $request, array $options): void {
        $time = round(microtime(true) - $this->timers[$id], 4);
        unset($this->timers[$id]);
        
        $uri = "{$request->getUri()->getHost()}/{$request->getUri()->getPath()}";
        
        $message = "{$request->getMethod()}@{$uri} Request took {$time} seconds";
        
        $this->logger->info($message);
    }
}

use ExeQue\Guzzle\Spy\Contracts\CanCreateRequestIds;
use ExeQue\Guzzle\Spy\Contracts\Spy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Ramsey\Uuid\Uuid;

class CustomSpy implements Spy, CanCreateRequestIds
{
    public function before(string $id, RequestInterface $request, array $options): void {
        ...
    }
    
    public function after(string $id, ResponseInterface $response, RequestInterface $request, array $options): void {
        ...
    }
    
    public function createRequestId(RequestInterface $request, array $options): string {
        return (string)Uuid::uuid4();
    }
}