如何使用 Guzzle 6 配置默认查询参数?

How do I configure default query parameters with Guzzle 6?

从 5 迁移到 6,我 运行 遇到了麻烦,找不到相关文档。

Guzzle 文档在这里,http://guzzle.readthedocs.io/en/latest/quickstart.html#creating-a-client,我们可以添加的站点 "any number of default request options"。

我想随每个请求发送 "foo=bar"。例如:

$client = new Client([
    'base_uri' => 'http://google.com',
]);

$client->get('this/that.json', [
    'query' => [ 'a' => 'b' ],
]);

这将在 http://google.com/this/that.json?a=b

上生成 GET

如何修改客户端构造使其产生:

http://google.com/this/that.json?foo=bar&a=b

感谢您的帮助!

好的,到目前为止,这在这里有效:

        $extraParams = [
            'a' => $config['a'],
            'b' => $config['b'],
        ];

        $handler = HandlerStack::create();
        $handler->push(Middleware::mapRequest(function (RequestInterface $request) use ($extraParams) {

            $uri  = $request->getUri();
            $uri .= ( $uri ? '&' : '' );
            $uri .= http_build_query( $extraParams );

            return new Request(
                $request->getMethod(),
                $uri,
                $request->getHeaders(),
                $request->getBody(),
                $request->getProtocolVersion()
            );
        }));

        $this->client = new Client([
            'base_uri' => $url,
            'handler' => $handler,
            'exceptions' => false,
        ]);

谁知道怎么让它看起来不那么凶险,谢谢!

github 中提出的解决方案看起来很丑陋。这看起来并没有好多少,但至少更具可读性并且也有效。如果有人知道为什么不应该使用,我希望得到反馈:

$query = $uri . '/person/id?personid=' . $personid . '&name=' . $name;    
return $result = $this->client->get(
  $query
  )
  ->getBody()->getContents();

我找到了一个很好的解决方案 here

基本上,第一个参数数组中定义的任何内容都会成为客户端 config 的一部分。

这意味着你可以在初始化时这样做:

$client = new Client([
    'base_uri' => 'http://google.com',
    // can be called anything but defaults works well
    'defaults' => [
        'query'  => [
            'foo' => 'bar',
        ]
    ]
]);

那么,使用客户端时:

$options = [
    'query'  => [
        'nonDefault' => 'baz',
    ]
];

// merge non default options with default ones
$options = array_merge_recursive($options, $client->getConfig('defaults'));

$guzzleResponse = $client->get('this/that.json', $options);

值得注意的是 array_merge_recursive 函数附加到嵌套数组而不是覆盖。如果您计划更改默认值,则需要不同的效用函数。当默认值不可变时,它工作得很好。

基于@Saeven 的回答和@VladimirPak 的评论的“less sinister-looking”示例。

        $query_defaults = [
            'a' => $config['a'],
            'b' => $config['b'],
        ];

        $handler = \GuzzleHttp\HandlerStack::create();
        $handler->push(\GuzzleHttp\Middleware::mapRequest(function (\Psr\Http\Message\RequestInterface $request) use ($query_defaults) {

            $query = \GuzzleHttp\Psr7\Query::parse($request->getUri()->getQuery());
            $query = array_merge($query_defaults, $query);

            return $request->withUri($request->getUri()->withQuery(\GuzzleHttp\Psr7\Query::build($query)));

        }));

        $this->client = new \GuzzleHttp\Client([
            'base_uri' => $url,
            'handler' => $handler,
            'exceptions' => false,
        ]);

不过我不确定 sinister-looking 有多少。哈哈