PHPackages                             exeque/guzzle-spy - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. exeque/guzzle-spy

ActiveLibrary[HTTP &amp; Networking](/categories/http)

exeque/guzzle-spy
=================

Spy middleware for Guzzle

0.2.0(1y ago)05MITPHPPHP ^8.2

Since Mar 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ExeQue/guzzle-spy)[ Packagist](https://packagist.org/packages/exeque/guzzle-spy)[ RSS](/packages/exeque-guzzle-spy/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Guzzle Spy Middleware
=====================

[](#guzzle-spy-middleware)

This middleware is designed to be used with Guzzle. It allows you to spy on requests and responses made by Guzzle. This is useful for testing and logging.

It is a simple middleware that reports request and response information to a spy class.

Installation
------------

[](#installation)

You can install Guzzle Spy Middleware using Composer:

```
composer require exeque/guzzle-spy
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

Guzzle Spy Middleware can be used by adding it to a Guzzle client.

```
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]);
```

### Custom Spy

[](#custom-spy)

You can create a custom spy by implementing the `ExeQue\Guzzle\Spy\Contracts\Spy` interface.

```
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);
    }
}
```

If you need more fine-grained control over the request id, you can implement the `ExeQue\Guzzle\Spy\Contracts\CanCreateRequestIds` interface.

The `createRequestId` method is called before the request is sent. It is passed the request and options array, and it should return a unique identifier for the request.

```
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();
    }
}
```

Testing
-------

[](#testing)

You can run the tests using Pest:

```
composer test
```

License
-------

[](#license)

Dedent is open-sourced software licensed under the [MIT license](LICENSE.md).

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance42

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

479d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23560353?v=4)[Morten Harders](/maintainers/ExeQue)[@ExeQue](https://github.com/ExeQue)

---

Top Contributors

[![ExeQue](https://avatars.githubusercontent.com/u/23560353?v=4)](https://github.com/ExeQue "ExeQue (10 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/exeque-guzzle-spy/health.svg)

```
[![Health](https://phpackages.com/badges/exeque-guzzle-spy/health.svg)](https://phpackages.com/packages/exeque-guzzle-spy)
```

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[neuron-core/neuron-ai

The PHP Agentic Framework.

2.0k656.1k38](/packages/neuron-core-neuron-ai)[illuminate/http

The Illuminate Http package.

11937.9M6.9k](/packages/illuminate-http)[tencentcloud/tencentcloud-sdk-php

TencentCloudApi php sdk

3741.3M45](/packages/tencentcloud-tencentcloud-sdk-php)[guzzlehttp/guzzle-services

Provides an implementation of the Guzzle Command library that uses Guzzle service descriptions to describe web services, serialize requests, and parse responses into easy to use model structures.

25711.0M191](/packages/guzzlehttp-guzzle-services)[eslazarev/wildberries-sdk

Wildberries OpenAPI clients (generated).

273.0k](/packages/eslazarev-wildberries-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
