如何在slim框架中为休息api添加http响应状态。在这段代码中,我必须通过数据库获取值,如果没有数据发现它将显示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>";
});
答案 0 :(得分:0)
这里我发布一些函数来获取响应状态和一个api示例来回应响应...希望它有用。
function verifyRequiredParams($required_fields) {
$error = false;
$error_fields = "";
$request_params = array();
$request_params = $_REQUEST;
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
$app = \Slim\Slim::getInstance();
parse_str($app->request()->getBody(), $request_params);
}
foreach ($required_fields as $field) {
if (!isset($request_params[$field]) ||
strlen(trim($request_params[$field])) <= 0) {
$error = true;
$error_fields .= $field . ', ';
}
}
if ($error) {
$response = array();
$app = \Slim\Slim::getInstance();
$returncode = "3";
$returnmessage = 'Some Fields Are Empty, Required
field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
$response['returncode'] = $returncode;
$response['returnmessage'] = $returnmessage;
$response['returndatacount'] = 0;
$response['returndata'] = array();
echoRespnse(400, $response);
$app->stop();
}
}
function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response);
}
function authenticate(\Slim\Route $route) {
$headers = apache_request_headers();
$response = array();
$app = \Slim\Slim::getInstance();
if (isset($headers['BLAuth'])) {
$db = new BLICKXDbHandler();
$api_key = $headers['BLAuth'];
if (!$db->isValidApiKey($api_key)) {
$returncode = "4";
$returnmessage = "Authentication Fail, Wrong
Authorization Header Define";
$response['returncode'] = $returncode;
$response['returnmessage'] = $returnmessage;
$response['returndatacount'] = 0;
$response['returndata'] = array();
echoRespnse(200, $response);
$app->stop();
} else {
}
} else {
$returncode = "4";
$returnmessage ="Authentication Fail, No Authorization
Header Define";
$response['returncode'] = $returncode;
$response['returnmessage'] = $returnmessage;
$response['returndatacount'] = 0;
$response['returndata'] = array();
echoRespnse(200, $response);
$app->stop();
}
}
$app->post('/getinfo', 'authenticate', function() use ($app) {
verifyRequiredParams(array('usertype'));
$usertype = $app->request()->post('usertype');
$response = array();
$db = new BLICKXDbHandler();
$result = array();
if(isset($usertype) && $usertype=="fr")
{
$result = $db->getFranchisedetails();
}
else if(isset($usertype) && $usertype=="op")
{
$result = $db->getOperatordetails();
}
if(count($result)==0)
{
$returncode = "1";
$returnmessage = "No Data Found";
$response['returncode'] = $returncode;
$response['returnmessage'] = $returnmessage;
$response['returndatacount'] = 0;
$response['returndata'] = array();
}
else
{
$response = array();
$returncode = "0";
$returnmessage = "Data Listed Sucessfully";
$alldata = array();
$kk=0;
if(isset($usertype) && $usertype=="fr")
{
foreach ($result as $key => $task) {
$franchise_id = $task["franchise_id"];
$franchise_name = $task["franchise_name"];
$alldata[$kk]['id'] = $franchise_id;
$alldata[$kk]['name'] = $franchise_name;
$kk++;
}
}
答案 1 :(得分:0)
您可以使用您的响应对象返回json数据和http状态 即使没有添加内容类型标题,它也会被您的响应对象添加。
$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);
//send json data wih http status
return $response->withJson($data,200);
//Where 200 is the http status
}
else{
$message=array("message"=>"empty data")
return $response->withJson($message,404);
//Where 404 is not found http status for example
}
});