How to set custom request headers (e.g., User-Agent) in a Guzzle request in PHP?

To set custom request headers like User-Agent in a Guzzle request in PHP, you can make use of the headers option when sending a request.

Here's an example of how to set the User-Agent header using Guzzle:

use GuzzleHttp\Client; // Create a new Guzzle client instance $client = new Client(); // Send a request with custom headers $response = $client->request('GET', 'https://example.com/api', [ 'headers' => [ 'User-Agent' => 'Your Custom User-Agent', // Add any other custom headers here if needed ], ]); // Get the response body $body = $response->getBody()->getContents(); // Do something with the response...

In the example above, the User-Agent header is set to "Your Custom User-Agent". You can replace it with your desired User-Agent value. You can also add more custom headers in the same manner.

Make sure you have installed Guzzle HTTP client using Composer before using the code above:

composer require guzzlehttp/guzzle

That's it! You can now send requests with custom headers using Guzzle in PHP.