【发布时间】:2018-02-16 16:30:07
【问题描述】:
如何在 slim 框架中添加 http 响应状态到 rest api。在这段代码中,如果没有找到数据,我必须通过数据库获取值,它将显示 http 状态响应
<?php
$app->get('/api/view', function() {
//call connection file
require_once('dbconnect.php');
//array for JSON response
$query = "select * from firm order by firmId";
$result = $mysqli->query($query);
// code node
while($row = $result->fetch_assoc())
{
// temp user array
$data[] = $row;
}
if (isset($data))
{
header('Content-Type: application/json');
echo json_encode($data);
}
});
//display single row
$app->get('/api/view/{firmId}', function($request, $response) {
require_once('dbconnect.php');
$firmId = $request->getAttribute('firmId');
$query = "select * from firm where firmId = $firmId";
$result = $mysqli->query($query);
$data[] = $result->fetch_assoc();
header('Content-Type: application/json');
echo json_encode($data)."</br>" ."</br>";
});
【问题讨论】: