29

Is there a way I can print out the full request as a string before or after it is sent?

$res = (new GuzzleHttp\Client())->request('POST', 'https://endpoint.nz/test', [ 'form_params' => [ 'param1'=>1,'param2'=>2,'param3'=3 ] ] );

how can I view that request as a string? (not the response)

The reason is, my request is failing and returning a 403, and I want to know what exactly is being sent; as the same request works when using PostMan.

kjones
  • 1,339
  • 1
  • 13
  • 28
Nicekiwi
  • 4,567
  • 11
  • 49
  • 88

3 Answers3

42

As per Guzzle documentation there is debug option, here is the link from guzzle documentation http://guzzle.readthedocs.org/en/latest/request-options.html#debug

$client->request('GET', '/get', ['debug' => true]);
Mohammed Safeer
  • 20,751
  • 8
  • 75
  • 78
  • 7
    The problem with this is that you're stuck if your application does anything to the output render, as you won't see anything. And you can't capture it without some needlessly convoluted rerouting of stdout. Oh and providing a phpstream to a log file gives a curl request error. Is guzzle designed to be as opaque as possible? – Michael Mallett Dec 18 '17 at 01:15
  • 2
    Putting [ob_start](http://php.net/manual/en/function.ob-start.php) and [ob_get_clean](http://php.net/manual/en/function.ob-start.php) around the `request` can get you the results of debug into a variable instead of standard out. – Tin Can Jan 12 '18 at 19:43
  • 8
    Again, needlessly convoluted and difficult to use without spending another few hours looking for an answer. It shouldn't be this difficult to debug something. I would have thought you could get this information using xdebug but it appears impossible. – Michael Mallett Jan 31 '18 at 04:01
  • 10
    This does not print the body of the request and thus does not answer the question. – Lars Nyström Jan 24 '20 at 13:51
10

According to a comment in this github issue, you can use the history middleware to store/output the request/response info.

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;

$container = [];
$history = Middleware::history($container);

$stack = HandlerStack::create();
// Add the history middleware to the handler stack.
$stack->push($history);

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

$client->request('POST', 'http://httpbin.org/post',[
    'body' => 'Hello World'
]);

// Iterate over the requests and responses
foreach ($container as $transaction) {
    echo (string) $transaction['request']->getBody(); // Hello World
}

A more advanced example here: http://docs.guzzlephp.org/en/stable/testing.html#history-middleware

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;

$container = [];
$history = Middleware::history($container);

$stack = HandlerStack::create();
// Add the history middleware to the handler stack.
$stack->push($history);

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

$client->request('GET', 'http://httpbin.org/get');
$client->request('HEAD', 'http://httpbin.org/get');

// Count the number of transactions
echo count($container);
//> 2

// Iterate over the requests and responses
foreach ($container as $transaction) {
    echo $transaction['request']->getMethod();
    //> GET, HEAD
    if ($transaction['response']) {
        echo $transaction['response']->getStatusCode();
        //> 200, 200
    } elseif ($transaction['error']) {
        echo $transaction['error'];
        //> exception
    }
    var_dump($transaction['options']);
    //> dumps the request options of the sent request.
}
kjones
  • 1,339
  • 1
  • 13
  • 28
armyofda12mnkeys
  • 3,214
  • 2
  • 32
  • 39
6

Mohammed Safeer's answer is the correct one, but to make it easier for folks who just set debug to true and got a bunch of text rendered in the middle of their script execution, you can also do the following:

$debug = fopen("path_and_filename.txt", "a+");
$client->request('GET', '/get', ['debug' => $debug ]);

This will output the debugging steam to a given file and not "interrupt" the execution of the request.

dearsina
  • 4,774
  • 2
  • 28
  • 34