How to send a send request to RESTserver api in php-Codeigniter?

I am trying to make a POST request in my CodeIgniter RestClient controller to insert data into my RestServer, but it looks like my POST request is incorrect.

Here is my RestClient POST client request in the controller:

$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); 

This is my RestServer controller: patient

 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')); } } 

This is user_model

 public function insertVisit($insertdata) { $this->db->insert('visit',$insertdata); } 
+6
source share
2 answers

Finally, I came up with a solution, I used PHP cURL to send a mail request to my REST server.

Here is my RESTclient POST request request

  $data = array( 'patient_id' => '1', 'department_name' => 'a', '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; 
+6
source

you can debug your code. try printing the global variable $ _POST. and I think this can solve your problem just use $this->input->post instead of $this->post instead

0
source

Source: https://habr.com/ru/post/979491/


All Articles