How to set default header in Guzzle?

Correct, the old method has been marked as @deprecated. Here is the new suggested method of setting default headers for multiple requests on the client.

// enter base url if needed
$url = ""; 
$headers = array('X-Foo' => 'Bar');

$client = new Guzzle\Http\Client($url, array(
    "request.options" => array(
       "headers" => $headers
    )
));

If you are using Guzzle v=6.0.*

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

read the doc, there are more options.


$client = new Guzzle\Http\Client();

// Set a single header using path syntax
$client->setDefaultOption('headers/X-Foo', 'Bar');

// Set all headers
$client->setDefaultOption('headers', array('X-Foo' => 'Bar'));

See here:

http://docs.guzzlephp.org/en/5.3/clients.html#request-options

Tags:

Php

Guzzle