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();
但我面臨像未定義的方法等其他錯誤。 請幫幫我。
謝謝您的回答。問題依然存在。我認爲它是因爲$ args。 我真的需要嗎?或使用($ app)@iturki –
@ mehrdad-pedramfar我不這麼認爲。你在使用Slim 3還是2? – iTurki
版本3.我想出來!問題是$ args。 –