How to set a custom User-Agent header for a cURL request in PHP?

To set a custom User-Agent header for a cURL request in PHP, you can use the CURLOPT_USERAGENT option of the cURL library. Here's an example:

$url = "https://example.com/api"; $userAgent = "MyCustomUserAgent/1.0"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); $response = curl_exec($ch); curl_close($ch); // Process the response echo $response;

In the above code, the CURLOPT_USERAGENT option is set using curl_setopt. The custom user agent string "MyCustomUserAgent/1.0" is assigned to the User-Agent header of the cURL request.

Make sure to replace the $url variable with your desired endpoint and modify the $userAgent variable according to your custom User-Agent string.