I’m making a cURL post request with the following script which posts to a form processing script:
$url = "http://www.example.com/process_script.php";
$referer = "http://www.example.com";
$agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11";
$postdata = array('message' => 'Hello, World!', 'name' => 'Bob');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_exec($ch);
It works fine on some urls. Also, when I post it to my test script (which just outputs the referer and agent and postdata) it works just fine.
Some servers however give me this bizarre “The server could not understand your query” error though. It’s not a 403, because when I remove the user agent option, I do get a 403. Is there something wrong with the user agent string? It’s pulled directly from my actual browser.
Are there more options I need to set to emulate a real browser?
I’m not sure, but give this a try instead:
The reason for doing this is because if you post the form using the url-encoded string returned by http_build_query() the form is posted using the enctype
application/x-www-form-urlencoded. When you use an array, it results inmultipart/form-data.It may be that some forms are expecting
urlencodeddata and do not understand (or decline to process) forms send inmultipart/form-dataencoding.About
CURLOPT_POSTFIELDS: