PHP: Set cURL User Agent

This is a small tutorial on how to use a custom User Agent with cURL in PHP.

To set a custom User Agent, we will need to make use of the CURLOPT_USERAGENT option. As you probably already know, PHP allows you to set cURL-related options using the curl_setopt function.

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_USERAGENT, "My User Agent" );

As described in the manual, CURLOPT_USERAGENT allows you to set the contents of the “User-Agent: ” header, which will be attached to your HTTP request.


In some cases, servers will disallow requests that contain unidentified user agents. In other cases, certain features will be disabled (in the past, I’ve come across errors about my browser being out of date). By setting a custom user agent, you can fool these servers into thinking that your cURL request is coming from a legitimate up-to-date browser. An example of imitating Firefox:

$userAgent = 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0';

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_USERAGENT, $userAgent );

The above user agent will fool the server into thinking that I am using Firefox version 31.0.

Other examples of common user agents:

  • Chrome: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2
  • Internet Explorer 10: Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)
  • Internet Explorer 6: Mozilla/4.08 (compatible; MSIE 6.0; Windows NT 5.1)
  • Googlebot: Googlebot/2.1 (+
  • Bingbot: Mozilla/5.0 (compatible; bingbot/2.0; +
  • IE 11: Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
  • Opera: Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14
  • Safari: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A
  • Twitter: Twitterbot/1.0
  • Facebook: facebookexternalhit/1.1 (+

As you can see, the possibilities are endless and you can pretty much fool any website that relies on the user agent!

Don’t Trust User Agents

As proven by the example above, you can never trust User Agent data. It can be easily spoofed or imitated with less than two lines of code. Example:

$userAgent = $_SERVER['HTTP_USER_AGENT'];

The $userAgent variable above will contain whatever the client has set. i.e. It cannot be relied upon.