且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何发送一个post请求到REST服务器api在php-Codeigniter?

更新时间:2023-02-26 13:53:03

解决方案,我使用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;