我正在使用codeigniter构建自己的REST Web服务,该应用程序具有名为“ calls ” 的资源,并且可以通过POST方法访问它,
“ 调用 ”函数通过使用以下内容类型的Curl调用通过json对象接收四个参数[id,manager,department,level]作为json对象:application / json
然后我正在使用codeigniter form_validation()库来验证请求,然后在form_validation()上没有错误的情况下返回响应数组
以下是通话功能代码
function calls_post() {
$this->load->model('api/central');
$this->load->library('form_validation');
//validation rules
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('manager', 'manager', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');
$this->form_validation->set_rules('level', 'level', 'required|numeric');
if ($this->form_validation->run() === FALSE) {
$employeeId = $this->form_validation->error('id') ? $this->form_validation->error('id') : $this->post('id');
$manager = $this->form_validation->error('manager') ? $this->form_validation->error('manager') : $this->post('manager');
$department = $this->form_validation->error('department') ? $this->form_validation->error('department') : $this->post('department');
$level = $this->form_validation->error('level') ? $this->form_validation->error('level') : $this->post('level');
$response = array(
'status' => FALSE,
'error' => 'We don\'t have data to display, Minimum information required to process the request is missing',
'authenticated' => true,
'id' => $employeeId,
'manager' => $manager,
'department' => $department,
'level' => $level
);
$this->response($response, 200);
} else {
$response = $this->central->calls_get($this->post('id'), $this->post('manager'), $this->post('department'), $this->post('level'));
$this->response($response);
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?:)
$_POST = json_decode(file_get_contents("php://input"), true);
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');
if($this->form_validation->run() == TRUE)
{
$id = $this->input->post('id');
$department = html_escape($this->input->post('department'));
echo $id."<br>".$department;
}
else
{
echo "false";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3854 次 |
最近记录: |