我可能会偏离基础,但我一直在尝试整个下午在这个凹陷的PHP框架教程中运行curl post命令.我不明白的是PHP应该如何解释我的POST,它总是作为一个空数组出现.
curl -i -X POST -d '{"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
Run Code Online (Sandbox Code Playgroud)
(那里的斜线只是为了让我看起来不像白痴,但是我使用PHP 5.2在Windows上执行此操作,也在Linux服务器上执行此操作,与Linux卷曲相同的版本)
必须有一些我缺少的东西,因为它看起来非常简单,这篇文章不是正确的解释,如果是的话,一切都会很好.
这就是我得到的回报:
HTTP/1.1 409 Conflict
Date: Fri, 01 May 2009 22:03:00 GMT
Server: Apache/2.2.8 (Win32) PHP/5.2.6
X-Powered-By: PHP/5.2.6
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
{"screencast":{"id":null,"subject":null,"body":null,
"dataUrl":null,"dataMedium":null,"createdOn":null,"author":null}}
小智 120
通常,该参数-d被解释为表格编码.你需要-H参数:
curl -v -H "Content-Type: application/json" -X POST -d '{"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
Run Code Online (Sandbox Code Playgroud)
Emi*_*l H 106
Jordans分析为什么没有填充$ _POST-数组是正确的.但是,你可以使用
$data = file_get_contents("php://input");
Run Code Online (Sandbox Code Playgroud)
只需检索http正文并自行处理.请参阅PHP输入/输出流.
从协议的角度来看,这实际上更加正确,因为无论如何你还没有真正处理http多部分表单数据.另外,在发布请求时使用application/json作为内容类型.
Jor*_*nes 18
我相信你得到一个空数组,因为PHP期望发布的数据采用Querystring格式(key = value&key1 = value1).
尝试将curl请求更改为:
curl -i -X POST -d 'json={"screencast":{"subject":"tools"}}' \
http://localhost:3570/index.php/trainingServer/screencast.json
Run Code Online (Sandbox Code Playgroud)
并看看这是否有帮助.
Chr*_*ler 13
您需要设置一些额外的标志,以便curl将数据作为JSON发送.
$ curl -H "Content-Type: application/json" \
-X POST \
-d '{"JSON": "HERE"}' \
http://localhost:3000/api/url
Run Code Online (Sandbox Code Playgroud)
-H:自定义标头,下一个参数应该是标头-X:自定义HTTP动词,下一个参数应该是动词-d:将下一个参数作为HTTP POST请求中的数据发送