在ZF2 Rest API中卷曲POST请求为空

时间:2014-09-24 20:25:17

标签: post curl zend-framework2

我正在测试我在本地主机上运行的ZF2 Rest模块,方法是从同一个盒子发送curl POST请求。

curl -i -X POST -H "Content-Type: Application/json" -d '{username":"xyz","password":"xyz"}' http://localhost/api/login

在相应的控制器和操作中,我尝试返回POST参数,但总是返回一个空数组

var_dump($this->getRequest()); // returns:  array(0){}
var_dump($_POST);              // returns:  array(0){}

如果我使用

从POST切换到GET
curl -i -G -H "Content-Type: Application/json" -d '{username":"xyz","password":"xyz"}' http://localhost/api/login

它实际上似乎有用

var_dump($_GET); // returns: array(1) {["{"username":"xyz","password":"xyz"}"]=>string(0) ""}

为什么POST请求无法传递/提取参数?

1 个答案:

答案 0 :(得分:2)

PHP仅填充$_POST表单urlencoded POST数据。您已明确将内容类型设置为JSON,因此访问它的PHP方式为:

file_get_contents("php://input");

在ZF2中,我相信你想要:

$this->getRequest()->getContent();

在实践中,您可能希望通过json_decode()运行此功能。

相关问题