简体   繁体   English

php curl发布请求

[英]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? 有人可以告诉我为什么没有列出type=1吗?

The POST variables are sent in the body of the request, this is not part of the headers. POST变量在请求的正文中发送,这不是标头的一部分。

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. 顺便说一句,当您将CURLOPT_POSTFIELDS设置为数组时,curl将正文作为multipart / form-data发送,并且相应地将Content-Type设置为multipart / form-data。

You should not set the Content-Type header yourself, as it breaks the request. 您不应该自己设置Content-Type标头,因为它会破坏请求。

Or set CURLOPT_POSTFIELDS as a string (eg http_build_query(array('type' => 1)); ) to avoid curl from sending POST as multipart/form-data. 或将CURLOPT_POSTFIELDS设置为字符串(例如http_build_query(array('type' => 1)); ),以避免curl将POST作为多部分/表单数据发送。

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

POST fields are not included in the header, they are included in the body. POST字段不包含在标题中,而是包含在正文中。

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. cURL在发送正文之前正在等待服务器以HTTP/1.1 100 Continue消息响应,因为它已经发送了Expect: 100-continue标头。

Is there more code missing from your question? 您的问题中还缺少其他代码吗? Because the Content-Length: 140 header is wrong if there isn't (should be 6)... 因为如果没有,则Content-Length: 140标头是错误的(应该为6)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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