Shipping couriers error on pac and sedex

I would like your help in the following errors, the first is in the pac, it returns the value, until there all right, but it also brings:

Fatal error: Uncaught exception 'Exception' with message 'Serialization of' SimpleXMLElement 'is not allowed' in [no active file]

Returns the correct transaction value but also returns this fatal error and I don't understand why.

Already in Sedex it always brings me the error -5, I looked at the API dos post office and as for the service codes, as well... they are the same, I can not understand why it returns me this error -5. I googled but found nothing about this error.

My Form

<form method="POST" action="carrinhoo.php" enctype="">
   <select name="frete" class="form-control">
      <option value="">Selecione...</option>
      <option value="carta">Carta Registrada</option>
      <option value="pac">Pac</option>
      <option value="sedex">Sedex</option>
   </select>
   Digite seu cep:<br>
   <input type="text" class="form-control" name="cep"><br>
   <input type="hidden" class="form-control" name="acao" value="calcular">
   <input type="submit" value="Calcular" class="btn btn-success">
    Valor frete: <?php echo ($_SESSION['valor_frete'] == '6.00') ? number_format($_SESSION['valor_frete'],2,',','.') : $_SESSION['valor_frete'];?>
 </form>

Here the request is made:

if(isset($_POST['acao']) && $_POST['acao'] == 'calcular'){
        $frete = $_POST['frete'];
        $cep = strip_tags(filter_input(INPUT_POST, 'cep'));
        switch ($frete){
            case 'carta':
                $valor = '6.00';
                $_SESSION['valor_frete'] = '6.00';
            break;
            case 'pac':
                $valor = '41106';
                $prod = new Produto();
                $_SESSION['valor_frete'] = $prod->CalculaFrete($valor, 45350000, $cep, '0.50');
            break;
            case 'sedex':
                $valor = '40010';
                $prod = new Produto();
                $_SESSION['valor_frete'] = $prod->CalculaFrete($valor, 45350000, $cep, '0.50');
                var_dump($_SESSION['valor_frete']);
            break;    
        }
    }

Here my method with the API of the post office

public function CalculaFrete($cod_servico, $cep_origem, $cep_destino, $peso, $altura = '2', $largura = '11', $comprimento = '16', $valor_declarado = '0.50'){
        $correios = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?"."nCdEmpresa=&sDsSenha=&sCepOrigem=".$cep_origem.
               "&sCepDestino=".$cep_destino."&nVlPeso=".$peso."&nCdFormato=1&nVlComprimento=".$comprimento."&nVlAltura=".$altura.
               "&nVlLargura=".$largura."&sCdMaoPropria=n"."&nVlValorDeclarado=".$valor_declarado."&sCdAvisoRecebimento=n".
               "&nCdServico=".$cod_servico."&nVlDiametro=0&StrRetorno=xml";

        $xml = simplexml_load_string(file_get_contents($correios));
        if($xml->cServico->Erro == '0'){
            return $xml->cServico->Valor;
        }else{
            var_dump($xml);
            return false;
        }
    }
Author: Victor Stafusa, 2016-10-19

2 answers

After hours fuçando I managed to solve the 2 problems, in fact they were not so difficult but it took about 5 hours to find out what was happening. About the error -5 that gave in Sedex has to be with the exception of values, in the case of simple Sedex which is what I use it has the following limits of values 10 real and 10.000, ie it does not send products with the value 10.000. In the case of Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file], this was already more difficult, it seems that from what I've been reading until php 5.3 did not occur this type of error in the mail webservice, as I use version 5.6 happens this exception.

Simplexml_load_string () cannot be serialized. An attempt to do so will result in a runtime failure, throwing an exception. If you store such an object you will receive a post-execution error.

Summarizing could not pass the object directly, and yes as a string, so I did it.

$xml = simplexml_load_string(file_get_contents($correios));

        if($xml->cServico->Erro == '0'){
            $xm = (string)$xml->cServico->Valor;
            return $xm;
        }else{
            return false;
        }
 1
Author: Alison Paulo, 2016-10-20 10:34:37

Erro -5 it is the declared value greater than R$ 10.000, 00.

All webservice errors...

CodE    Erro

10  CEP de destino est  temporariamente sem entrega domiciliar. A entrega ser  efetuada na agˆncia indicada no Aviso de Chegada que ser  entregue no endere‡o do destinat rio

11  CEP de destino est  sujeito a condi‡äes especiais de entrega pela ECT e ser  realizada com o acr‚scimo de at‚ 7 (sete) dias £teis ao prazo regular.

-40 Para definicao do preco deverao ser informados, tambem, o comprimento e a largura e altura do objeto em centimetros (cm).

-11 Para defini‡Æo do pre‡o deverÆo ser informados, tamb‚m, o comprimento, a largura e altura do objeto em cent¡metros (cm).

-32 A soma resultante do comprimento + o dobro do diƒmetro nÆo deve superar a 200 cm.

-23 A soma resultante do comprimento + largura + altura nÆo deve superar a 200 cm.

-43 A soma resultante do comprimento + largura nao deve superar a 120 cm.

-33 Sistema temporariamente fora do ar. Favor tentar mais tarde.

-38 Servi‡o indispon¡vel para este c¢digo administrativo.

7   Localidade de destino nÆo abrange o servi‡o informado

-36 Cliente nÆo possui contrato vigente com os Correios.

6   Localidade de origem nÆo abrange o servi‡o informado

-37 Cliente nÆo possui servi‡o ativo em seu contrato.

-10 Precifica‡Æo indispon¡vel para o trecho informado

-7  O Valor Declarado ‚ obrigat¢rio para este servi‡o

-5  O Valor Declarado nÆo deve exceder R$ 10.000,00

-42 O comprimento nao pode ser inferior a 16 cm.

-30 O comprimento nÆo pode ser inferior a 18 cm.

-28 O comprimento nÆo pode ser maior que 105 cm.

-22 O comprimento nÆo pode ser inferior a 16 cm.

-15 O comprimento nÆo pode ser maior que 105 cm.

-9  Este servi‡o nÆo aceita Aviso de Recebimento

-6  Servi‡o indispon¡vel para o trecho informado

8   Servi‡o indispon¡vel para o trecho informado

-41 O comprimento nao pode ser maior que 60 cm.

-34 C¢digo Administrativo ou Senha inv lidos.

-44 A largura nao pode ser inferior a 11 cm.

-31 O diƒmetro nÆo pode ser inferior a 5 cm.

-29 O diƒmetro nÆo pode ser maior que 91 cm.

-20 A largura nÆo pode ser inferior a 11 cm.

-16 A largura nÆo pode ser maior que 105 cm.

9   CEP inicial pertencente a µrea de Risco.

-45 A largura nao pode ser maior que 60 cm.

-17 A altura nÆo pode ser maior que 105 cm.

-18 A altura nÆo pode ser inferior a 2 cm.

7   Servi‡o indispon¡vel, tente mais tarde

-39 Peso excedido para o formato envelope

-8  Este servi‡o nÆo aceita MÆo Pr¢pria

99  Outros erros diversos do .Net

-1  C¢digo de servi‡o inv lido

0   Processamento com sucesso

-888    Erro ao calcular a tarifa

-3  CEP de destino inv lido

-26 Informe o comprimento.

-2  CEP de origem inv lido

-24 Comprimento inv lido.

-12 Comprimento inv lido.

-27 Informe o diƒmetro.

-25 Diƒmetro inv lido

-13 Largura inv lida.

-35 Senha incorreta.

-14 Altura inv lida.

-4  Peso excedido
 -1
Author: Jamesfk, 2018-07-04 21:49:51