Set the User Agent With PHP cURL

By  on  

A few months back, I shared with you how to download the contents of a URL and execute a HTTP POST transmission using PHP cURL. For security purposes, some hosts require that a common user agent be present in the POST. If an unacceptable user agent is given, the POST is ignored. Luckily, cURL allows us to "spoof" the server using any user agent we choose:

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

A little sneaky but it could get you out of a jam at some point!

Recent Features

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

Incredible Demos

  • By
    Introducing MooTools ScrollSide

    This post is a proof of concept post -- the functionality is yet to be perfected. Picture this: you've found yourself on a website that uses horizontal scrolling instead of vertical scrolling. It's an artistic site so you accept that the site scrolls left to right.

  • By
    CSS Text Overlap

    One of the important functions of CSS is to position elements. Margin, padding, top, left, right, bottom, position, and z-index are just a few of the major players in CSS positioning. By using the above spacing...

Discussion

  1. Thanks for this tip. Was working on a project and found your tip very handy.

  2. Thanks for this! Was needed for a wikipedia curl request.

  3. Thanks for this, it for me too.

  4. hi..
    How to check whether a user agent string is supported by all the urls or not ? I have tried many urls with different user agent strings but I couldn’t find any user agent which is supported by all urls. When a user agent string is not supported, I get 0 (zero) as http response code and null as mime type. Please help.

  5. Thanks, without setting user agent I was getting this error:

    at nl.bitwalker.useragentutils.Browser.isInUserAgentString(Browser.java:154)

  6. Brilliand

    “for security purposes”… that’s incredibly weak security. I think it’s more common for the website to be doing useragent sniffing (to tailor the response to the browser), and simply not bothering to support uncommon useragents.

  7. perfect – thank for the tip!

  8. MJ

    Thank, that helped!

  9. i get error code like this. curl_setopt() expects parameter 1 to be resource, null given in /home/xxxx/xxxxx

  10. Try this…

    function scrap($url)	{
    	$curl = curl_init($url);
    
    	curl_setopt($curl, CURLOPT_USERAGENT, "My User Agent Name");
    	curl_setopt($curl, CURLOPT_FAILONERROR, true);
    	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    
    	$html = curl_exec($curl);
    	curl_close($curl);
    
    	return $html;
    }
    

    You are getting the error as curl_setopt() required first parameter as a resource returned by the curl_init($url);

  11. This, in fact, just got me out of a jam.

  12. Jim

    Almost a full 10 years later and this post just really helped me out! Thanks!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!