It looks like curl_setopt_array is different from just multiple invocations of curl_setopt. Consider this script:
$ch = curl_init('http://www.stackoverflow.com/');
[options]
curl_exec($ch);
var_dump(curl_getinfo($ch));
Now it sends a proper request if [options] are one of these:
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array('a' => 'b'),
));
or
curl_setopt_array($ch, array(
CURLOPT_POSTFIELDS => array('a' => 'b'),
));
or
curl_setopt($ch, CURLOPT_POSTFIELDS, 'a=b');
curl_setopt($ch, CURLOPT_POST, 1);
BUT NOT this way:
curl_setopt_array($ch, array(
CURLOPT_POSTFIELDS => array('a' => 'b'),
CURLOPT_POST => true,
));
It seems that the content-length is reset if CURLOPT_POST is set after CURLOPT_POSTFIELDS. Except it works ok if set with curl_setopt instead of curl_setopt_array.
Why is this?
When you specify
CURLOPT_POST, the post is sent asapplication/x-www-form-urlencoded.But, from the
curl_setoptmanual page:So when you do
The data is being set as
mulpart/form-data(by settingCURLOPT_POSTFIELDSto an array) and then reset asapplication/x-www-form-urlencoded(by settingCURLOPT_POSTto true).The other examples work because you’re not changing the type once the data is set.