What is the purpose of setting a specific browser user-agent in PHP?

Setting a specific browser user-agent in PHP can be useful when you want to mimic a different browser or device when making HTTP requests. This can be helpful for testing purposes or when you want to access content that is only available to certain user agents. By setting a specific user-agent, you can make your PHP script behave as if it is being accessed from a different browser or device.

<?php
$url = 'https://example.com';
$userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>