cURL - Consuming webservice with PHP

4

I have the following code:

$url_data = "http://localhost:8080/sistema/webservice/agenda/consultarHorariosDisponiveis";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt( $ch, CURLOPT_URL, $url_data);
curl_setopt ($ch, CURLOPT_POST, true);

$parametros = array(
     "idUnidade" => '3',
     "intervaloDuracao" => '10',
     "dataInicio" => "02/08/2016",
     "dataFim" => "04/08/2016",
     "horaInicio" => "08:00",
     "horaFim" => "22:00"
);

curl_setopt ($ch, CURLOPT_POSTFIELDS, $parametros);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
curl_close($ch);

Testing the webservice with a SoapUI program is working fine, however, when I try to get a return with php with this code, in the console of my application in java, it is giving NullPointer, at first the parameters are not going correctly , so I think it's just a detail in my code, any suggestions?

    
asked by anonymous 19.07.2016 / 14:37

2 answers

1

I had the same problem and solved using the link function, which generates a string in URL format. In the following way:

$url_data = "http://localhost:8080/sistema/webservice/agenda/consultarHorariosDisponiveis";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt( $ch, CURLOPT_URL, $url_data);
curl_setopt ($ch, CURLOPT_POST, true);

$parametros = array(
     "idUnidade" => '3',
     "intervaloDuracao" => '10',
     "dataInicio" => "02/08/2016",
     "dataFim" => "04/08/2016",
     "horaInicio" => "08:00",
     "horaFim" => "22:00"
);

$data_Post = http_build_query($parametros); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $data_Post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
curl_close($ch);

I hope I have helped.

    
19.07.2016 / 19:33
1

If your PHP is earlier than 5.2.0, you will have to use curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parametros)) as @touchmx said.

Otherwise, it should work normally.

Source: here

    
21.06.2018 / 20:11