Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I profile Guzzle 6 requests?

Tags:

php

guzzle6

I'm trying to profile the requests made to an API server from a PHP client using Guzzle (v 6).

In the Guzzle 5.3 there is this complete and before event handling.

class GuzzleProfiler implements SubscriberInterface
{
    public function getEvents()
    {
        return [
            'before'   => ['onBefore'],
            'complete' => ['onComplete']
        ];
    }

    public function onBefore(BeforeEvent $event, $name)
    {
         start_profiling();
    }

    public function onComplete(CompleteEvent $event, $name)
    {
         end_profiling();
    }
}

But how do I do this in v6?

like image 491
Petra Barus Avatar asked Aug 07 '15 04:08

Petra Barus


People also ask

How do you send a POST request on GuzzleHttp?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

How do I get responses from GuzzleHttp?

Both request and response messages can contain a body. You can check to see if a request or response has a body using the getBody() method: $response = GuzzleHttp\get('http://httpbin.org/get'); if ($response->getBody()) { echo $response->getBody(); // JSON string: { ... } }

What is guzzle request?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...


1 Answers

Just found it using Middleware. Here's the code.

class Profiler {

    /**
     * @return callable
     */
    public static function profile() {
        return function(callable $handler) {
            return function(\Psr\Http\Message\RequestInterface $request, array $options) use ($handler) {
                start_profiling();
                return $handler($request, $options)->then(function(\Psr\Http\Message\ResponseInterface $response) use ($token) {
                    end_profiling();
                    return $response;
                });
            };
        };
    }
}

And then attach the profiler like this.

$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(Profiler::profile());
$client = new \GuzzleHttp\Client([
   'handler' => $stack
]);
like image 127
Petra Barus Avatar answered Oct 07 '22 02:10

Petra Barus