簡體   English   中英

如何在php-Codeigniter中向REST服務器api發送帖子請求?

[英]How to send a post request to the RESTserver api in php-Codeigniter?

我一直在嘗試在我的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',
            '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。 而且我認為這可以解決你的問題只需使用$this->input->post而不是$this->post

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM