php + curl无法设置post方法

I try to make a post request with php and curl. Here is my code

//PHP 5.3.5 and curl: 7.18.2
$ch = curl_init();
if(!empty($save_cookie)){
    curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie);
}else{
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}

curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/post.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, !$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$postResult = curl_exec($ch);

if (curl_errno($ch)) {
    return false;
}

curl_close($ch);
return $postResult;

In http://localhost/post.php, I write

print_r($_SERVER);

The result return of curl is always

[REQUEST_METHOD] => GET

Remove the CURLOPT_NOBODY option and it will work. Or place it above the CURLOPT_POST line.

I think I have encountered this once, when trying to get just the header of a response. Setting

curl_setopt($ch, CURLOPT_NOBODY, true);

effectively instructs curl to issue a HEAD request, which is not a POST request. I think there is no way to just get the header from a POST (and just drop the connection after receiving the header). As a side effect, setting CURLOPT_NOBODY to false sets the request type to GET...

Do you really need the CURLOPT_NOBODY flag?

Try to move the

curl_setopt($ch, CURLOPT_NOBODY, !$body);

line right before the

curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);

line.

There's an interesting post at the curl/set_opt page, shedding some light on this behaviour:

If your POST data seems to be disappearing (POST data empty, request is being handled by the server as a GET), try rearranging the order of CURLOPT_POSTFIELDS setting with CURLOPT_NOBODY. CURLOPT_POSTFIELDS has to come AFTER CURLOPT_NOBODY setting because if it comes after it wipes out the header that tells your URL target that the request is a POST not a GET.