How to set default header in Guzzle?

Jürgen Paul picture Jürgen Paul · Jul 20, 2013 · Viewed 67.7k times · Source
$baseUrl = 'http://foo';
$config = array();
$client = new Guzzle\Http\Client($baseUrl, $config);

What's the new way to set the default header for Guzzle without passing it as a parameter on every $client->post($uri, $headers)?

There's $client->setDefaultHeaders($headers) but it's deprecated.

setDefaultHeaders is deprecated. Use the request.options array to specify default request options

Answer

tasmaniski picture tasmaniski · Aug 10, 2015

If you are using Guzzle v=6.0.*

$client = new GuzzleHttp\Client(['headers' => ['X-Foo' => 'Bar']]);

read the doc, there are more options.