Get Deadline with Postal Web Service

Asked

Viewed 1,184 times

2

I am trying to get the delivery time dynamically using the Webservice Post, for this I am using this documentation.

If you enter the their test page, it is possible to enter the PAC service, for example, 4510 and the source and destination ceps to get the return on an XML page.

I found that this form makes a simple POST by copying it to my localhost. But by trying to do it without the form, but by using curl did not succeed.

My code so far is:

$data['nCdServico'] = '4510';
$data['sCepOrigem'] = '36572008';
$data['sCepDestino'] = '36700000';

$url = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrazo';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$resp = curl_exec($ch);
curl_close($ch);

var_dump($resp);exit;

But the return is false

I would like a help to know what I’m forgetting so that the return is the data of the sending deadline and not just the boolean false.

  • Why aren’t you wearing SoapClient?

  • Just lack of knowledge on how to use it.

  • That’s a good argument, I’ll write an answer

  • Whoa, I was having lunch, I’ll check in there. Thanks.

1 answer

4


PHP has the native library SoapClient that abstract many aspects of the consumption of a webservice.

To connect with WS, it’s simple:

$client = new SoapClient('http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL');

You must pass as parameter the WSDL address, which is the description of how to consume the web service.

You can also pass some parameters as below:

$client = new SoapClient(
    'http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx?WSDL',
    array( 
        // Stuff for development. 
        'trace' => 1, 
        'exceptions' => true, 
        'cache_wsdl' => WSDL_CACHE_NONE
    ) 
);
  • trace: enable debug functions (all start with "__").
  • Exceptions: will enable errors to be released via exceptions (Soapfault)
  • cache_wsdl: with the parameter WSDL_CACHE_NONE, there will be no cache.

All parameters are focused for development.

After, the use is as simple as:

$data['nCdServico'] = '4510';
$data['sCepOrigem'] = '36572008';
$data['sCepDestino'] = '36700000';

$response = $client->CalcPrazo($data);

The result of the consultation is:

object(stdClass)#2 (1) {
  ["CalcPrazoResult"]=>
  object(stdClass)#3 (1) {
    ["Servicos"]=>
    object(stdClass)#4 (1) {
      ["cServico"]=>
      object(stdClass)#5 (8) {
        ["Codigo"]=>
        int(4510)
        ["PrazoEntrega"]=>
        string(1) "5"
        ["EntregaDomiciliar"]=>
        string(1) "S"
        ["EntregaSabado"]=>
        string(1) "N"
        ["Erro"]=>
        string(0) ""
        ["MsgErro"]=>
        string(0) ""
        ["obsFim"]=>
        string(0) ""
        ["DataMaxEntrega"]=>
        string(10) "20/08/2018"
      }
    }
  }
}

Being a stdClass, properties must be accessed as an object (as it is an Objet):

echo "Data máxima de entrega: ".$response->CalcPrazoResult->Servicos->cServico->DataMaxEntrega;

Exit:

Maximum delivery date: 20/08/2018

  • I will try to use with SOAP and I already give you answer, the first moment gave a fatal error that the class Soapcliente does not exist in my localhost. I’ll see to install it.

  • @Davidalves windows or linux?

  • Linux, but I’m able to use it on my remote server.

  • Very good the result, I will now use more soapclient for sure! Thank you very much

Browser other questions tagged

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