php-guzzleHow to make a GET request with headers with PHP Guzzle?
Using PHP Guzzle, you can make a GET request with headers by creating a GuzzleHttp\Client object and passing the headers as an array 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
$client = new GuzzleHttp\Client();- This creates aGuzzleHttp\Clientobject.$response = $client->request('GET', 'http://example.com', [- This calls therequestmethod on theGuzzleHttp\Clientobject, passing in the request type (GET), the URL (http://example.com), and an array of headers.'headers' => [- This is the array of headers that will be sent with the request.'X-Foo' => 'Bar',- This is an example of a header that will be sent with the request.]);- This closes the array of headers and therequestmethod.
Helpful links
More of Php Guzzle
- How to keep alive with Guzzle in PHP?
- How to make HTTP2 request with PHP Guzzle?
- How to use multipart/form-data with PHP Guzzle?
- How to fire and forget with PHP Guzzle?
- PHP Guzzle post request example
- How to download a file with Guzzle in PHP?
- How to convert a Guzzle request to a cURL request in PHP?
- How to set a timeout for a request with PHP Guzzle?
- How to use PHP Guzzle to make a batch request?
- How to stream with PHP Guzzle?
See more codes...