简体   繁体   中英

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?

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 (eg http_build_query(array('type' => 1)); ) to avoid curl from sending POST as multipart/form-data.

POST数据在请求正文中发送,而不在标头中发送。

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)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM