Get deadline with the Web Service of the Post Office

0

I'm trying to get the deadline dynamically using the Post Office WebService, so I'm using this documentation .

If you enter the test page , you can enter the CAP, for example, 4510 and the source and destination ceps to get the return on an XML page.

I've found that this form does a simple POST by copying it to my localhost. But when I try to do it without the form, but 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 some help to know what I'm forgetting so that the return is the data of the submission deadline and not just the boolean false.

    
asked by anonymous 13.08.2018 / 16:55

1 answer

2

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

To connect with WS, it's simple:

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

You should pass 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 thrown via exceptions (SoapFault)
  • cache_wsdl : with the parameter WSDL_CACHE_NONE , there will be no cache.

All parameters are focused on development.

Next, use is as simple as:

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

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

The result of the query 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 stdClass , the properties should be accessed as an object (therefore, it is an objet):

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

Output:

  

Maximum delivery date: 20/08/2018

    
13.08.2018 / 17:10