首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >string 6,get请求字符串

string 6,get请求字符串
EN

Stack Overflow用户
提问于 2015-09-14 07:12:13
回答 2查看 35.7K关注 0票数 26

有没有一种方法可以在发送之前或之后将完整的请求打印为字符串?

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

如何将该请求视为字符串?(不是响应)

原因是,我的请求失败并返回403,我想知道发送的确切内容;因为在使用PostMan时,同样的请求也是有效的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-15 00:55:15

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

代码语言:javascript
复制
$client->request('GET', '/get', ['debug' => true]);
票数 34
EN

Stack Overflow用户

发布于 2018-09-01 02:01:55

根据a comment in this github issue的说法,您可以使用历史中间件来存储/输出请求/响应信息。

代码语言:javascript
复制
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
}

这里有一个更高级的例子:http://docs.guzzlephp.org/en/stable/testing.html#history-middleware

代码语言:javascript
复制
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.
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32555417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档