为了解释如何做使用nc
http://linux.die.net/man/1/nc
HTTP下班
$ nc -l 8888
启动虚拟服务器听在8888
使用jQuery(通过XHR IMPL)发送GET请求
$.get("http://localhost:8888", { a :1 ,b: 2})
nc w乌尔德打印什么XHR向服务器发送到stdout
$nc -l 8888
GET /?a=1&b=2&_=1383234919249 HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Accept: */*
Origin: http://stackoverflow.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
DNT: 1
Referer: http://stackoverflow.com/questions/19710815/understanding-how-xmlhttprequest-sends-data-to-a-server
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
因此,PHP解析GET /?a=1&b=2&_=1383234919249
到$_GET
POST
使用nc
来记录POST
POST/HTTP/1.1
Host: localhost:8888
Connection: keep-alive
Content-Length: 7
Accept: */*
Origin: http://stackoverflow.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded
DNT: 1
Referer: http://stackoverflow.com/questions/19710815/understanding-how-xmlhttprequest-sends-data-to-a-server
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4
a=1&b=2
在这里你可以看到Content-Type: application/x-www-form-urlencoded
,它告诉由浏览器发送的HTTP主体是形式编码
作为结果,PHP解析a=1&b=2
到阵列$_POST
WHY php://input
不能看到POST BODY
根据http://php.net/manual/en/wrappers.php.php
php://input
是一个流,可以只读一次
以下是来自php文档
注意:使用php://输入打开的流只能读取一次; 流不支持查找操作。但是,根据SAPI实现 ,可能会打开另一个php://输入 流并重新开始读取。这仅在请求主体 数据已被保存时才有可能。通常,POST请求的情况是 ,但不包括其他请求方法,例如PUT或PROPFIND。
非常感谢您的回答。读完之后我还有一些问题。所以只有'GET'是特殊的,所有其他请求方法都将它们的数据编码到请求内容中?如何读取请求内容以解码数据(根据指定的内容类型)?你谈论一个帖子正文,每个请求类型是否只有一个HTTP正文或一个正文? – danijar
@danijar标题中的Content-Type。 HTTP正文是HTTP头后的其余数据,POST和PUT有正文,但它们是相同的。在PHP中,它们都是流(php://输入) – farmer1992
好了,现在更清晰了。我可以提供一个自定义的HTTP主体,而不管请求方法如何,例如用'GET'请求发送JSON编码的信息?我想知道这是否有限制。 – danijar