PHP Guzzle how to fetch request object

Viewed 112

I am using Guzzle library and I would like to fetch what is request object that is being sent to endpoint, so I can debug some API problems. Not sure how to get it and where?

Using this line to make a request and return data, response object is easily obtainable, what about request?

   return $this->client->request('put', $endpoint, $options)
1 Answers

The only way to capture the Request with guzzle is to create the request yourself and send it to guzzle.

  $request = new Request('put', $endpoint, $headers, $body, "1.1");
  $response = $this->client->send($request);
  return array($request, $response);

If you are using promises and want the request on resolution, you need to use a callback.

  $request = new Request('put', $endpoint, $headers, $body, "1.1");
  return $this->client
    ->sendAsync($request)
    ->then(
      function (ResponseInterface $response) use ($request) {
        return array($request, $response);
      },
      function (RequestException $e) use ($request) {
        return array($request, $e);
      }
    );
Related