[英]How to add http response status to rest api in slim framework
How to add http response status to rest api in slim framework. 如何在苗条框架中将http响应状态添加到rest api。 In this code i have to fetch values through database if no data found it will show http status response 在此代码中,如果没有找到数据,我必须通过数据库获取值,它将显示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>";
});
here i post some function to get response status and one api example to echo response... hope it helpfull. 在这里,我发布了一些获取响应状态的函数和一个响应示例的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++;
}
}
You can Just Use your response object to return json data and http status even without adding the content type header, It will be added by your response object. 即使不添加内容类型标头,也可以仅使用响应对象返回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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.