R. 久蔵
R. 久蔵

Reputation: 187

php curl post request

I wrote this code to send a post request

$ch = curl_init("http://www.exemple.com");
curl_setopt($ch, CURLOPT_COOKIE, "PHPSESSID=32chars; prsess_******=32chars; login_******=55chars");
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("type" => "1"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded; charset=UTF-8"));
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($ch);
print $head = curl_getinfo($ch, CURLINFO_HEADER_OUT);}
curl_close($ch);

But I'm getting this as a header

POST / HTTP/1.1
User-Agent: Mozilla/5.0 ...
Host: www.exemple.com
Accept: */*
Referer: http://www.exemple.com/page.php
Cookie: PHPSESSID=32chars; prsess_******=32chars; login_******=55chars
Content-Length: 140
Expect: 100-continue
Content-type: application/x-www-form-urlencoded; charset=UTF-8; boundary=----------------------------0636ec3c1d17

Can someone tell me why type=1 is not listed?

Upvotes: 0

Views: 7666

Answers (3)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99879

The POST variables are sent in the body of the request, this is not part of the headers.

The body of the request is sent just after the headers, separated by a blank line.

BTW when you set CURLOPT_POSTFIELDS as an array, curl is sending the body as multipart/form-data, and it sets the Content-Type to multipart/form-data accordingly.

You should not set the Content-Type header yourself, as it breaks the request.

Or set CURLOPT_POSTFIELDS as a string (e.g. http_build_query(array('type' => 1));) to avoid curl from sending POST as multipart/form-data.

Upvotes: 6

DaveRandom
DaveRandom

Reputation: 88647

POST fields are not included in the header, they are included in the body.

cURL is waiting for the server to respond with a HTTP/1.1 100 Continue message before it sends the body, because it has sent a Expect: 100-continue header.

Is there more code missing from your question? Because the Content-Length: 140 header is wrong if there isn't (should be 6)...

Upvotes: 1

Marc B
Marc B

Reputation: 360562

POST data is sent in the body of the request, not in the headers.

Upvotes: 2

Related Questions