php-在Guzzle 6中设置默认查询字符串

升级到Guzzle6后,我不知道如何为客户端设置默认查询字符串.

我有以下几点:

$client = new \GuzzleHttp\Client( [
    'base_uri' => 'http://api.example.org/',
    'query'   => ['key' => 'secretKey']
] );
$client->get( 'extract', ['query' => ['url' => $url]] );

在此请求中,我的默认查询sting key = secretKey被忽略.

我该如何运作?

解决方法:

Guzzle v6不支持客户端选项中的默认查询字符串.需要中间件.

$handler = new HandlerStack();
$handler->setHandler(new CurlHandler());

//Add access token
$handler->unshift(Middleware::mapRequest(function(RequestInterface $request) {
    return $request->withUri(Uri::withQueryValue($request->getUri(), 'key', 'value'));
}));

//Create client
$this->client = new Client([
    'base_uri' => ''
    'handler' => $handler
]);

参见https://github.com/guzzle/guzzle/issues/1138.

上一篇:composer 报错: Error while processing content unencoding: Unknown failure within decompression softwar


下一篇:Unlock Login Keyring时忘记密码解决办法