我一直在尝试在我的CodeIgniter RestClient控制器中发出POST请求以在我的RestServer中插入数据,但看起来我的POST请求是错误的.
这是我在控制器中的RestClient POST请求:
$method = 'post'; $params = array('patient_id' => '1','department_name' => 'a','patient_type' => 'b'); $uri = 'patient/visit'; $this->rest->format('application/json'); $result = $this->rest->{$method}($uri,$params);
这是我的RestServer的控制器:耐心
function visit_post() { $insertdata=array('patient_id' => $this->post('patient_id'),'department_name' => $this->post('department_name'),'patient_type' => $this->post('patient_type') ); $result = $this->user_model->insertVisit($insertdata); if($result === FALSE) { $this->response(array('status' => 'Failed')); } else { $this->response(array('status' => 'success')); } }
这是user_model
public function insertVisit($insertdata) { $this->db->insert('visit',$insertdata); }
最后我提出了一个解决方案,我使用PHP cURL向我的REST服务器发送一个帖子请求.
这是我的RESTclient POST请求
$data = array( 'patient_id' => '1','patient_type' => 'b' ); $data_string = json_encode($data); $curl = curl_init('http://localhost/patient-portal/api/patient/visit'); curl_setopt($curl,CURLOPT_CUSTOMREQUEST,"POST"); curl_setopt($curl,CURLOPT_HTTPHEADER,array( 'Content-Type: application/json','Content-Length: ' . strlen($data_string)) ); curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); // Make it so the data coming back is put into a string curl_setopt($curl,CURLOPT_POSTFIELDS,$data_string); // Insert the data // Send the request $result = curl_exec($curl); // Free up the resources $curl is using curl_close($curl); echo $result;