加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

如何在php-Codeigniter中向REST服务器api发送帖子请求?

发布时间:2020-05-27 04:57:38 所属栏目:PHP 来源:互联网
导读:我一直在尝试在我的CodeIgniter RestClient控制器中发出POST请求以在我的RestServer中插入数据,但看起来我的POST请求是错误的. 这是我在控制器中的RestClient POST请求: $method = post;$params = array(patient_id = 1, department_nam

我一直在尝试在我的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;

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读