php-guzzleHow to add a header in PHP Guzzle?
Adding a header in PHP Guzzle is a simple process. To do this, you need to create a GuzzleHttp\Client
object and pass an array of headers to the request
method.
$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com', [
'headers' => [
'X-Foo' => 'Bar',
'X-Baz' => 'Qux'
]
]);
The output of the above code will be a GuzzleHttp\Psr7\Response
object.
Code explanation
GuzzleHttp\Client
: This is the class used to create a client object.request
: This is the method used to make a request.headers
: This is the array of headers to be sent with the request.X-Foo
andX-Baz
: These are the header names.Bar
andQux
: These are the header values.
Helpful links
More of Php Guzzle
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...