Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default query string in Guzzle 6

Tags:

php

guzzle

After upgrading to Guzzle6, I can't figure out how to setup default query string for the Client.

I have the following:

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

In this request my default query sting key=secretKey is being ignored.

How can I make it work?

like image 670
Websirnik Avatar asked Jun 21 '26 05:06

Websirnik


1 Answers

Guzzle v6 does not support default query strings within the client options. Middleware is required.

$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
]);

See https://github.com/guzzle/guzzle/issues/1138 for source.

like image 195
Jason Avatar answered Jun 23 '26 00:06

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!