2
I would like your help in the following mistakes, the first is in Pac, it return the value, until then everything 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 value of the transaction but also returns this fatal error and do not understand why.
Already in Sedex it always brings me the error -5, I looked in the API of the post office and as for the codes of the services, well... they are the same, I can not understand why returns me this error -5. I googled but found nothing about that mistake.
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 requisition 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’s my method with the Post Office API
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;
}
}
A hint instead of
strip_tags(filter_input(INPUT_POST, 'cep'))
usepreg_replace('/[^0-9]/', '', $_POST['cep']);
, so he will remove anything that is not (^
) a number from 0 to 9 (0-9
).– Junior Zancan
face in good catch the return in json much easier d process
– Jasar Orion