Get deadline with Post Office Web Service

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

If you enter their Testing page , you can enter the PAC service, for example 4510 and the source and destination ceps to get the return in an XML page.

I checked that this form makes a simple POST by copying it to my localhost. But when trying to do it without the form, but yes using curl I 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 am forgetting so that the return is the data of the sending deadline and not just the false boolean.

Author: David Alves, 2018-08-13

1 answers

PHP has the Native librarySoapClient which abstracts 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 a parameter the address of the WSDL, 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 activate that errors will be launched 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 query result 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 (since, it is an objet):

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

Output:

Maximum delivery date: 20/08/2018

 4
Author: Gabriel Heming, 2018-08-13 16:36:24