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

标签 php guzzle guzzle6

从 5 迁移到 6,我遇到了一个障碍,找不到相关的文档。

Guzzle 文档在这里,http://guzzle.readthedocs.io/en/latest/quickstart.html#creating-a-client ,我们可以添加“任意数量的默认请求选项”的站点。

我想在每个请求中发送“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,
        ]);

如果有人知道如何使它看起来不那么险恶,我会说谢谢!

关于php - 如何使用 Guzzle 6 配置默认查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38755846/

相关文章:

php - 使用 GuzzleHttp REST API 客户端在 PHP 中进行非阻塞调用

php - 是否可以通过 HTTP/2 设置 Guzzle + Pool?

php - 使用 curl 或 guzzle 的 laravel passport oauth 功能

php - Symfony 2 - Guzzle 6.X HTTP |获得 body react

php - 使用 pecl 在 linux 上为 lampp 设置 memcached

c# - Razor 语法 PHP 等价物

PHP 使用多个 while 循环仅检索 1 行

php - 带有 Bootstrap 的搜索结果页面中的 Wordpress 自定义布局

php - Guzzle 的行为不像 CURL