2014-09-24 75 views
0

我正在通過發送來自同一個盒子的curl POST請求來測試我在本地主機上運行的ZF2 Rest模塊。curl POST請求在ZF2 Rest API中爲空

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請求無法傳遞/提取參數?

回答

2

PHP僅爲表單urlencoded POST數據填充$_POST。您明確設置內容類型爲JSON,所以PHP的方式來訪問,這將是:

file_get_contents("php://input"); 

在ZF2,我相信你想:

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

並在實踐中,你會可能要通過json_decode()運行。

+0

自己來到這個解決方案,但你是100%的權利。這是問題所在 – Pankrates 2014-09-24 23:10:49