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!
I love almost every part of being a tech blogger: learning, preaching, bantering, researching. The one part about blogging that I absolutely loathe: dealing with SPAM comments. For the past two years, my blog has registered 8,000+ SPAM comments per day. PER DAY. Bloating my database...
Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...
There are plenty of awesome new attributes we've gotten during the HTML5 revolution: placeholder, download, hidden, and more. Each of these attributes provides us a different level of control over an element on the page, but there's a new element attribute that allows...
Everyone loves the MooTools Accordion plugin but I get a lot of requests from readers asking me how to make each accordion item open when the user hovers over the item instead of making the user click. You have two options: hack the original plugin...
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!