headers

Summary
Associative array of headers to add to the request. Each key is thename of a header, and each value is a string or array of stringsrepresenting the header field values.
Types
array
Defaults
None
Constant
GuzzleHttp\RequestOptions::HEADERS
  1. // Set various headers on a request
  2. $client->request('GET', '/get', [
  3. 'headers' => [
  4. 'User-Agent' => 'testing/1.0',
  5. 'Accept' => 'application/json',
  6. 'X-Foo' => ['Bar', 'Baz']
  7. ]
  8. ]);

Headers may be added as default options when creating a client. When headersare used as default options, they are only applied if the request being createddoes not already contain the specific header. This includes both requests passedto the client in the send() and sendAsync() methods, and requestscreated by the client (e.g., request() and requestAsync()).

  1. $client = new GuzzleHttp\Client(['headers' => ['X-Foo' => 'Bar']]);
  2.  
  3. // Will send a request with the X-Foo header.
  4. $client->request('GET', '/get');
  5.  
  6. // Sets the X-Foo header to "test", which prevents the default header
  7. // from being applied.
  8. $client->request('GET', '/get', ['headers' => ['X-Foo' => 'test']]);
  9.  
  10. // Will disable adding in default headers.
  11. $client->request('GET', '/get', ['headers' => null]);
  12.  
  13. // Will not overwrite the X-Foo header because it is in the message.
  14. use GuzzleHttp\Psr7\Request;
  15. $request = new Request('GET', 'http://foo.com', ['X-Foo' => 'test']);
  16. $client->send($request);
  17.  
  18. // Will overwrite the X-Foo header with the request option provided in the
  19. // send method.
  20. use GuzzleHttp\Psr7\Request;
  21. $request = new Request('GET', 'http://foo.com', ['X-Foo' => 'test']);
  22. $client->send($request, ['headers' => ['X-Foo' => 'overwrite']]);