53
$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
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133

5 Answers5

60

If you are using Guzzle v=6.0.*

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

read the doc, there are more options.

tasmaniski
  • 4,767
  • 3
  • 33
  • 65
47
$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

Otti
  • 374
  • 5
  • 9
drj201
  • 1,085
  • 1
  • 9
  • 13
  • how do I do the same for Basic Auth username and pass? – Zhianc Jul 21 '13 at 04:16
  • 23
    In Guzzle 6, you can only set default options at client instantiation. If you had to work with an existing instance, you can't configure it anymore. See [What replaces client->setDefaultOption?](https://github.com/guzzle/guzzle/issues/1419). "Oh hey, let's make things less flexible, just because. Will look much [Enterprise](https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition)". Sigh. – Gras Double Apr 21 '17 at 23:18
  • How does this work when you want to do unit testing and you want to inject Client into your class? – Robert Johnstone Mar 11 '19 at 16:43
5

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
    )
));
stikman
  • 51
  • 1
  • 3
2

This works for me if you are doing it with drupal;

<?php
$url = 'https://jsonplaceholder.typicode.com/posts';

$client = \Drupal::httpClient();

$post_data = $form_state->cleanValues()->getValues();

$response = $client->request('POST', $url, [
    'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
    'form_params' => $post_data,
    'verify' => false,
]);

$body = $response->getBody()->getContents();
$status = $response->getStatusCode();

dsm($body);
dsm($status);
Novocaine
  • 4,692
  • 4
  • 44
  • 66
1

For setting default headers to a Guzzle client (if using the client as a base for multiple requests), its best to set up a middleware which would add the header on every request. Otherwise additional request headers will get overridden in new client requests.

For example:

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;

...

$handler = HandlerStack::create();
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
    return $request->withHeader('Authorization', "Bearer {$someAccessToken}");
}));

$client = new Client([
    'base_uri' => 'https://example.com',
    'handler' => $handler,
]);

Middlware Docs

Shay
  • 2,060
  • 2
  • 16
  • 22