php-guzzleHow to set a user agent in PHP Guzzle?
Setting a user agent in PHP Guzzle is easy. You can do it by passing a User-Agent header to the request.
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com', [
'headers' => [
'User-Agent' => 'MyApp/1.0'
]
]);
The output of the above code will be a GuzzleHttp\Psr7\Response object.
Code explanation
$client = new \GuzzleHttp\Client();- This creates a new Guzzle client.$response = $client->request('GET', 'http://example.com', [- This sends aGETrequest tohttp://example.com.'headers' => [- This sets the headers for the request.'User-Agent' => 'MyApp/1.0'- This sets theUser-Agentheader toMyApp/1.0.]);- This closes the array of headers.
Helpful links
More of Php Guzzle
- How to mock requests with Guzzle in PHP?
- How to make an asynchronous send with PHP Guzzle?
- How to send multiple requests with Guzzle in PHP?
- How to make HTTP2 request with PHP Guzzle?
- How to use multipart/form-data with PHP Guzzle?
- How to make an HTTP POST request with JSON using PHP Guzzle?
- How to catch a PHP Guzzle exception?
- PHP Guzzle post request example
- How to convert a Guzzle request to a cURL request in PHP?
- How to install PHP Guzzle without Composer?
See more codes...