Created
July 8, 2020 21:13
Star
You must be signed in to star a gist
Sistema de cálculo de fretes utilizando a API dos correios, PHP, HTML e Javascript.
This file contains 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 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> |
Olá, sou novato em programação,
Aqui não funcionou, copiei e colei o código. Testei localmente com xampp e no servidor infinityfree.
A fim de teste, isolei a parte do ajax e passei os dados do formulário para calcular.php via POST e deu:
Fatal error: Maximum execution time of 120 seconds exceeded
na linha $unparsedResult = file_get_contents($url);Então para teste, inclui echo "
".$url;o resultado do echo, se eu copiar e colocar no navegador, carrega o xml corretamente.
Como resolver?
Quando tu acessa o link gerado no $url
no navegador, abre normal?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Olá, sou novato em programação,
Aqui não funcionou, copiei e colei o código. Testei localmente com xampp e no servidor infinityfree.
A fim de teste, isolei a parte do ajax e passei os dados do formulário para calcular.php via POST e deu:
Fatal error: Maximum execution time of 120 seconds exceeded
na linha $unparsedResult = file_get_contents($url);
Então para teste, inclui echo "
".$url;
o resultado do echo, se eu copiar e colocar no navegador, carrega o xml corretamente.
Como resolver?