16

Ho passato tutta la giornata a cercare di capire questo problema. Pubblicare questo problema qui è la mia ultima speranza. Spero che qualcuno possa aiutare a continuare a lavorare sul mio primo lavoro.CodeIgniter Passaggio dei dati POST da RestClient a RestServer API

Quindi, POST funziona correttamente quando si passa direttamente i dati dalle mie viste al RestServer direttamente. Tuttavia, l'API RESTServer non è in grado di trovare i dati POST inviati dal RestClient.

Qui ci sono i frammenti:

RestClient API: 

     $ver = 'v1'; 
     $config = array('server'   => base_url()."v1", 
         'api_key'   => 'xxxxxx', 
         'api_name'  => 'X-API-KEY', 
         'http_user'  => 'admin', 
         'http_pass'  => 'xxxxx', 
         'http_auth'  => 'basic', 
       ); 

     $this->rest->initialize($config); 
     $this->rest->format('application/json'); 
     $param = array(
       'id' => $this->input->post('id'), // works fine here 
       'name' => $this->input->post('name') 
       ); 
     $user = $this->rest->post('employer/postNewProject', $param, 'json'); 


     //if (isset($user->errors)) show_404('admin'); 
     $this->rest->debug(); 

RestServer API

class Employer extends REST_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->lang->load("application"); 
     $this->load->library("users/auth"); 
     Datamapper::add_model_path(array(APPPATH."modules")); 
    } 

    public function postNewProject_post() 
    { 
     // I tired $this->post() and $this->input->post() but both not finding the data 
     $message = array("id" => $this->post("id"), "name" => $this->input->post("name")); 
     $this->response($message, 200); // 200 being the HTTP response code 
    } 
} 

risultati:

Response when using $this->post('id'); 
{"id":false} 

Response when using $this->post(); 
{"id":[]} 

Nota: Ho sostituito i posti richieste con i dati hardcoded, e ancora il RestServer non è in grado di ricevere i dati dal mio RestClient.

Se avete bisogno di me per fornire qualcos'altro, per favore chiedete.

Grazie in anticipo.

+0

Provare con il formato impostato con solo json come '$ this-> rest-> format ('json');' – jagad89

risposta

7

Utilizzare questo metodo per inviare i dati del client Rest al server di riposo utilizzando il metodo post.Leggi il commento su ogni linea

// initialize you setting 
    $config = array('server' => base_url() . "v1", 
      'api_key' => 'xxxxxx', 
      'api_name' => 'X-API-KEY', 
      'http_user' => 'admin', 
      'http_pass' => 'xxxxx', 
      'http_auth' => 'basic', 
     ); 
     $this->rest->initialize($config); 
// Set method of sending data 

     $method = 'post'; 
// create your param data 

     $param = array(
      'id' => $this->input->post('id'), // works fine here 
      'name' => $this->input->post('name') 
     ); 
// url where you want to send your param data. 

     $uri = 'employer/postNewProject'; 
     // set format you sending data 
     $this->rest->format('application/json'); 

// send your param data to given url using this 

     $result = $this->rest->{$method}($uri, $params); 

// codice per il controller employer/postNewProject

controller

function postNewProject() 
{ 
    $data=array(
     'id' => $this->post('id'), 
     'name' => $this->post('name') 
     ); 
     print_r($data); 
} 
-2

Sull'API RestServer, non avrà accesso ai dati POST, i dati POST dall'API RestClient vengono passati all'API RestServer tramite l'array $ param e l'API RestServer riceve quell'array. Nell'API RestServer dovresti essere in grado di accedere a tali valori tramite l'array $ param assumendo che le funzioni dell'API RestServer consumino l'array $ param e lo passino attraverso.

+0

Puoi mostrarmi come accedere a '$ param' sul RestServer? – Sobiaholic

+0

In CodeIgnitor, dovresti essere in grado di accedere al post vars tramite $ this-> post ('id'); Pertanto, sono portato a credere che forse il resto dell'URL richiesto potrebbe essere formattato male. Hai accesso al log del server RestAPI per vedere quale URL viene richiesto? – zbot

+0

Tramite il registro del server RestAPI si intendono i registri codeigniter giusti? L'ho controllato e non ci sono errori riguardo la mia API. – Sobiaholic

7

ho scoperto quando e il terzo perameter viene passato (o quando si rimuove il terzo parametro), quindi funziona.

//Not working 
    $user = $this->rest->post('employer/postNewProject', $param, 'json'); 
    //when i change to this ,it's working 
    $user = $this->rest->post('employer/postNewProject', $param.''); 
    //or 
    $user = $this->rest->post('employer/postNewProject', $param); 

se qualcuno ha una soluzione migliore in modo da poter assegnare taglie.

4

assicurarsi di ricevere i dati, utilizzare

echo '<pre> all data received: '.print_r($_REQUEST,1).'</pre>'; 

assicurarsi è dal post

echo '<pre> post data: '.print_r($_POST,1).'</pre>'; 
0

tipo contenuto/applicazione x-www-form-urlencoded usato per metodo post

$param = array(
      'id' => $this->input->post('id'), // works fine here 
      'name' => $this->input->post('name') 
      ); 

$user = $this->rest->post('employer/postNewProject', $param, 'application/x-www-form-urlencoded'); 

tipo contenuto json per metodo post RestServer API sarà cambiata per posta

public function postNewProject_post() 
{ 
    $entity = file_get_contents('php://input', 'r'); 
    print_r($entity); 
} 
Problemi correlati