2014-12-12 22 views
6

CodeIgniter RestClient denetleyicimde RestServer ürünüme veri eklemek için POST isteği yapmaya çalışıyorum, ancak POST isteğim yanlış gibi görünüyor. Bu benim RestServer en denetleyicisi olanphp-Codeigniter'da RESTserver api'ye bir posta gönderme isteği nasıl gönderilir?

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

: Burada

denetleyicisi benim RestClient POST isteği olduğunu hasta

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

Bu user_model

public function insertVisit($insertdata) 
{ 
    $this->db->insert('visit',$insertdata); 
} 
+0

sorunuzu belirtin benim RESTclient POST talebidir. Ne işe yaramıyor, ne gibi hatalar var ... – Kyslik

+0

@Kyslik CodeIgniter RestClient denetleyicimde CodeIgniter RestServer cihazıma veri eklemek için POST isteği yapmaya çalışıyorum, ancak veritabanımda hiçbir zaman veri eklemiyor. – Madhu

+0

"$ this-> response" öğesinden değer alıyor musunuz? Eğer öyleyse, gönderiyi verebilirsiniz. $ this-> response (dizi ($ _ POST)) – Linesofcode

cevap

6

$this->post yerine kullanımı çözebilir düşünüyorum Sonunda bir çözüm geldi, benim RESTserver için bir post isteği göndermek için PHP cURL kullandı.

İşte

$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; 
+0

[bağlantı] http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl – Madhu

0

kodunuzu ayıklayabilirsiniz. Global değişkeni $ _POST yazdırmayı deneyin. ve bu senin sorunun sadece $this->input->post yerine

İlgili konular