Created
July 8, 2020 21:13
-
-
Save zerobugs-oficial/376cb27004cbc5148ee6b5439803f805 to your computer and use it in GitHub Desktop.
Sistema de cálculo de fretes utilizando a API dos correios, PHP, HTML e Javascript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$variaveis_extras = http_build_query($_POST); | |
$url = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&sCdAvisoRecebimento=n&sCdMaoPropria=n&nVlValorDeclarado=0&nVlDiametro=0&StrRetorno=xml&nIndicaCalculo=3&nCdFormato=1&" . $variaveis_extras; | |
$unparsedResult = file_get_contents($url); | |
$parsedResult = simplexml_load_string($unparsedResult); | |
$retorno = array( | |
'preco' => strval($parsedResult->cServico->Valor), | |
'prazo' => strval($parsedResult->cServico->PrazoEntrega) | |
); | |
die(json_encode($retorno)); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Calcular Valor do Frete</title> | |
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> | |
</head> | |
<body> | |
<form id="formDestino" action=""> | |
<p> | |
<label for="">Cep de origem</label> | |
<input name="sCepOrigem" type="text"> | |
</p> | |
<p> | |
<label for="">Cep de destino</label> | |
<input name="sCepDestino" type="text"> | |
</p> | |
<p> | |
<label for="">Peso</label> | |
<input name="nVlPeso" type="text"> | |
</p> | |
<p> | |
<label for="">Comprimento</label> | |
<input name="nVlComprimento" type="text"> | |
</p> | |
<p> | |
<label for="">Altura</label> | |
<input name="nVlAltura" type="text"> | |
</p> | |
<p> | |
<label for="">Largura</label> | |
<input name="nVlLargura" type="text"> | |
</p> | |
<p> | |
<label for="">Serviço</label> | |
<select name="nCdServico" id=""> | |
<option value="04014">Sedex</option> | |
<option value="04510">PAC</option> | |
</select> | |
</p> | |
<p><button type="button" id="calcular">Calcular</button></p> | |
</form> | |
<p id="resultado"></p> | |
<script> | |
$('#calcular').click(function() { | |
let formSerialized = $('#formDestino').serialize(); | |
$.post('calcular.php', formSerialized, function(resultado) { | |
resultado = JSON.parse(resultado); | |
let valorFrete = resultado.preco; | |
let prazoEntrega = resultado.prazo; | |
$('#resultado').html(`O valor do frete é <b>R$ ${valorFrete}</b> e o prazo de entrega é <b>${prazoEntrega} dias úteis</b>.`); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quando tu acessa o link gerado no
$url
no navegador, abre normal?