Reputation: 4807
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.
Upvotes: 35
Views: 55531
Reputation: 950
Guzzle debug mode does not give all the info and its not formatted as a http request. And middleware is annoying to setup for all places you might want to debug.
Make a new php file that reconstructs the request from the info it receives.
And then send your guzzle request to that url instead and your raw http will be inside the response.
print-request.php
<?php
header('Content-Type: text/plain');
//uri
echo "$_SERVER[REQUEST_METHOD] $_SERVER[REQUEST_URI] $_SERVER[SERVER_PROTOCOL]\r\n";
//headers
foreach (getallheaders() as $name => $value) {
echo "$name: $value\r\n";
}
// body
echo "\r\n", file_get_contents('php://input');
Now you can debug every guzzle request by simply changing the url and seeing the response while stepping through with Xdebug.
Upvotes: 0
Reputation: 5212
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.
Upvotes: 7
Reputation: 3442
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.
}
Upvotes: 11
Reputation: 21545
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]);
Upvotes: 45