Set the User Agent With PHP cURL
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!
In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...
This is the hardest thing I've ever had to write, much less admit to myself. I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life. All of those feelings were very...
One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...
I'll be honest with you: I still haven't figured out if I like my MooTools teammate Guillermo Rauch. He's got a lot stacked up against him. He's from Argentina so I get IM'ed about 10 times a day about how great Lionel...
Thanks for this tip. Was working on a project and found your tip very handy.
Thanks for this! Was needed for a wikipedia curl request.
Thanks for this, it for me too.
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.
Thanks, without setting user agent I was getting this error:
at
nl.bitwalker.useragentutils.Browser.isInUserAgentString(Browser.java:154)
“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.
perfect – thank for the tip!
Thank, that helped!
i get error code like this.
curl_setopt()
expects parameter 1 to be resource,null
given in/home/xxxx/xxxxx
Try this…
You are getting the error as
curl_setopt()
required first parameter as a resource returned by thecurl_init($url)
;This, in fact, just got me out of a jam.
Almost a full 10 years later and this post just really helped me out! Thanks!