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\Client
object.$response = $client->request('GET', 'http://example.com', [
- This calls therequest
method on theGuzzleHttp\Client
object, 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 therequest
method.
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...