2016-05-28 43 views
0

Im使用Slim v3。通過作曲家安裝。 這裏是我的register.php文件:使用Slim v3無法獲得JSON POST數據

use \Psr\Http\Message\ServerRequestInterface as Request; 
use \Psr\Http\Message\ResponseInterface as Response; 

require 'vendor/autoload.php'; 


$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]); 


//-------------- Register ------------------ 
$app->post('/', function (Request $request, Response $response ,$args) use($app) { 

$json =$request->getParams(); 
$data = json_decode($json, true); 
$response->getBody()->write($data); 

return $response; 
}); 
$app->run(); 

當我通過郵遞員樣品後像的Json {"name":"jack", "age":"10", "gender":"male"} 我得到的RuntimeException錯誤Could not write to stream。 我已經使用$app->request()->post();$request->getParams();$request->getParsedBody();但我面臨像未定義的方法等其他錯誤。 請幫幫我。

回答

1

,我認爲你應該使用getBody()與請求,不getParams

$json =$request->getBody(); 
+0

謝謝您的回答。問題依然存在。我認爲它是因爲$ args。 我真的需要嗎?或使用($ app)@iturki –

+0

@ mehrdad-pedramfar我不這麼認爲。你在使用Slim 3還是2? – iTurki

+0

版本3.我想出來!問題是$ args。 –

1

可以使用

 
//-------------- Register ------------------ 
$app->post('/', function (Request $request, Response $response, $args) use ($app) { 
    $json =$request->getParams(); 
    return $response->withJson($json); 
});