Set request options after initialisation in guzzle 6 Set request options after initialisation in guzzle 6 php php

Set request options after initialisation in guzzle 6


The best option for Guzzle 6+ is to recreate the Client. Guzzle's HTTP client is now immutable, so whenever you want to change something you should create a new object.

This doesn't mean that you have to recreate the whole object graph, HandlerStack and middlewares can stay the same:

use GuzzleHttp\Client;use GuzzleHttp\HandlerStack;$stack = HandlerStack::create();$stack->push(Middleware::retry(/* ... */));$stack->push(Middleware::log(/* ... */));$client = new Client([    'handler' => $stack,    'base_url' => $url,]);// ...$newClient = new Client([    'handler' => $stack,    'base_url' => $url,    'auth' => [$username, $password]]);


You can send the 'auth' param, while constructing the client or sending the request.

$client = new Client(['base_url' => $url, 'auth' => ['username', 'password', 'digest']]);

OR

$client->get('/get', ['auth' => ['username', 'password', 'digest']]);

The other way is to rewrite the requestAsync method and add your custom logic in it. But there is no reason for that.