如何发送一个post请求到REST服务器api在php-Codeigniter? [英] How to send a post request to the RESTserver api in php-Codeigniter?
本文介绍了如何发送一个post请求到REST服务器api在php-Codeigniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试在我的CodeIgniter RestClient控制器中插入数据在我的RestServer中的POST请求,但似乎我的POST请求是错误的。
I've been trying to make a POST request in my CodeIgniter RestClient controller to insert data in my RestServer, but seems like my POST request is wrong.
这是控制器中的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控制器: patient
This is my RestServer's 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'));
}
}
这是 user_model / p>
This is user_model
public function insertVisit($insertdata)
{
$this->db->insert('visit',$insertdata);
}
推荐答案
解决方案,我使用PHP cURL向我的REST服务器发送一个post请求。
Finally I came up with a solution, I used PHP cURL to send a post request to my RESTserver.
这里是我的RESTclient POST请求 b
$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;
这篇关于如何发送一个post请求到REST服务器api在php-Codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文