To include headers in a Guzzle HTTP request in PHP, you can use the headers
option of the request
method. Here is an example of how to include headers in a Guzzle HTTP request:
use GuzzleHttp\Client;
// Create a new Guzzle client
$client = new Client();
// Define the headers
$headers = [
'Header1' => 'Value1',
'Header2' => 'Value2',
];
// Make a GET request with headers
$response = $client->request('GET', 'http://example.com/api', [
'headers' => $headers,
]);
// Get the response body
$body = $response->getBody()->getContents();
// Print the response body
echo $body;
In this example, the headers
array defines the headers you want to include in the request. You can add as many headers as required. Finally, the headers
option is passed to the request
method to include them in the request.