php Guzzle 6,获取请求字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32555417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:03:56  来源:igfitidea点击:

Guzzle 6, get request string

phphttpguzzleguzzle6

提问by Nicekiwi

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.

原因是,我的请求失败并返回 403,我想知道到底发送了什么;因为在使用 PostMan 时相同的请求有效。

采纳答案by Mohammed Safeer

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

根据 Guzzle 文档有调试选项,这里是来自 guzzle 文档的链接 http://guzzle.readthedocs.org/en/latest/request-options.html#debug

$client->request('GET', '/get', ['debug' => true]);

回答by armyofda12mnkeys

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

根据此 github 问题中的评论,您可以使用历史中间件来存储/输出请求/响应信息。

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

这里有一个更高级的例子: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.
}