Guzzle 6, get request string
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.
}
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'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.