2016-06-13 9 views
1

arbeiten I Formulardaten an einen Dienst zu stellen, aber es zeigt einige Fehler mit Formularparametern, dies ist mein curl Skriptbeispiel:curl Postformulardaten nicht

<?php 
//  header('Content-Type:application/json'); 

     $data = array(); 
     $data = json_decode(file_get_contents('php://input'), true); 


     $user= $data["username"]; 
     $pass= $data["password"]; 


     $form_data = array('username' => $user, 'password' => $pass); 
     $url = "http://localhost:8585/auth/session"; 


     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 75); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data); 

     $result = curl_exec($ch); 
     curl_close($ch); 

     echo "result $result"; 

auf der Service-Seite es einen Fehler erzeugen dies wie:

@FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded 

Antwort

1

diese Zeile ersetzen

curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data); 

mit dieser Linie

curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($form_data));

der Grund, warum Sie einen Fehler bekommen ist ein Array senden, aber was Sie brauchen, ist zu senden ein urlencoded String. Beziehen Sie sich darauf: http://php.net/manual/en/function.http-build-query.php