Curl - Consuming webservice with PHP

Asked

Viewed 2,076 times

3

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 program Soapui is working well, however, when I try to get a return with php with this code, in the console of my application in java, is giving Nullpointer, at first the parameters are not going correctly, then I believe it’s just a detail in my code, some suggestion?

  • I don’t understand your signage, if I may say thank you.

2 answers

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 shall function normally.

Source: here

1

I had the same problem and solved using the function http_build_query, which generates a string in URL format. As follows:

$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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.