2014-12-12 10 views
6

Ho cercato di fare una richiesta POST nel mio controller CodeIgniter RestClient per inserire dati nel mio RestServer, ma sembra che la mia richiesta POST sia sbagliata.Come inviare una richiesta di posta all'API RESTserver in php-Codeigniter?

Ecco la mia richiesta RestClient POST controller:

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

Questo è un controllore di mio RestServer: paziente

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

Questo è user_model

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

prega di formulare domande. Cosa non funziona, qual è l'errore ecc ... – Kyslik

+0

@Kyslik Ho cercato di fare una richiesta POST nel mio controller CodeIgniter RestClient per inserire dati nel mio RestServer CodeIgniter, ma non inserisce mai dati nel mio database. – Madhu

+0

Ricevi il valore da $ this-> response'? Se è così, potresti pubblicare il post. '$ this-> response (array ($ _ POST))' – Linesofcode

risposta

6

Infine mi si avvicinò con una soluzione, che ho usato PHP cURL per inviare una richiesta POST al mio RESTserver.

Ecco il mio RESTclient richiesta 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; 
+0

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

0

puoi eseguire il debug del tuo codice. prova a stampare la variabile globale $ _POST. e credo che questo possa risolvere il tuo problema basta usare $this->input->post invece di invece di $this->post