使用 Slim 返回 JSON 响应

Slim 可以使用 withJson(),它可以返回具有所需路由和方法的 JSON 响应

$app = new \Slim\App();
$app->get('/api/person', function ($request, $response, $args) {

$payload=[];
array_push($payload, array("name"=>"Bob"  ,"birth-year"=>1993));
array_push($payload, array("name"=>"Alice","birth-year"=>1995));

 return $response->withJson($payload,200);

});

$app->run();

尝试卷曲

`curl -X GET http://127.0.0.1/api/person`

它可以在 json 中返回一个数组

[{"name":"Bob","birth-year":1993},{"name":"Alice","birth-year":1995}]