2
First of all, I need to search the zip code down the street, which means I don’t have the zip code and I have the address, I need to find the zip code by the address. So please don’t link this question with Search Street by zip code
Problem
I have a PHP code that searches streets by zip code, but I need the opposite, I need you to search by street, city and state. This is possible?
Important Information for Troubleshooting
The Webservice Viacep
Offers ZIP and IBGE webservice for free, among the functions he also has to search the zip code by the street, but I can not adapt the functions in the code, error or does not work, nothing appears.
In addition to leaving the street search code by zip code, I will also leave the função
linked to how it works to search the street by the zip code.
Search Street by ZIP code
Exemplo: viacep.com.br/ws/UF/Cidade/Rua/json/
Current Code
<html>
<head>
<title>ViaCEP Webservice</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- Adicionando Javascript -->
<script type="text/javascript" >
function limpa_formulário_cep() {
//Limpa valores do formulário de cep.
document.getElementById('rua').value=("");
document.getElementById('bairro').value=("");
document.getElementById('cidade').value=("");
document.getElementById('uf').value=("");
document.getElementById('ibge').value=("");
}
function meu_callback(conteudo) {
if (!("erro" in conteudo)) {
//Atualiza os campos com os valores.
document.getElementById('rua').value=(conteudo.logradouro);
document.getElementById('bairro').value=(conteudo.bairro);
document.getElementById('cidade').value=(conteudo.localidade);
document.getElementById('uf').value=(conteudo.uf);
document.getElementById('ibge').value=(conteudo.ibge);
} //end if.
else {
//CEP não Encontrado.
limpa_formulário_cep();
alert("CEP não encontrado.");
}
}
function pesquisacep(valor) {
//Nova variável "cep" somente com dígitos.
var cep = valor.replace(/\D/g, '');
//Verifica se campo cep possui valor informado.
if (cep != "") {
//Expressão regular para validar o CEP.
var validacep = /^[0-9]{8}$/;
//Valida o formato do CEP.
if(validacep.test(cep)) {
//Preenche os campos com "..." enquanto consulta webservice.
document.getElementById('rua').value="...";
document.getElementById('bairro').value="...";
document.getElementById('cidade').value="...";
document.getElementById('uf').value="...";
document.getElementById('ibge').value="...";
//Cria um elemento javascript.
var script = document.createElement('script');
//Sincroniza com o callback.
script.src = '//viacep.com.br/ws/'+ cep + '/json/?callback=meu_callback';
//Insere script no documento e carrega o conteúdo.
document.body.appendChild(script);
} //end if.
else {
//cep é inválido.
limpa_formulário_cep();
alert("Formato de CEP inválido.");
}
} //end if.
else {
//cep sem valor, limpa formulário.
limpa_formulário_cep();
}
};
</script>
</head>
<body>
<!-- Inicio do formulario -->
<form method="get" action=".">
<label>Cep:
<input name="cep" type="text" id="cep" value="" size="10" maxlength="9"
onblur="pesquisacep(this.value);" /></label><br />
<label>Rua:
<input name="rua" type="text" id="rua" size="60" /></label><br />
<label>Bairro:
<input name="bairro" type="text" id="bairro" size="40" /></label><br />
<label>Cidade:
<input name="cidade" type="text" id="cidade" size="40" /></label><br />
<label>Estado:
<input name="uf" type="text" id="uf" size="2" /></label><br />
<label>IBGE:
<input name="ibge" type="text" id="ibge" size="8" /></label><br />
</form>
</body>
</html>
Summary
Practically need to adapt this PHP code, to use as CEP Unknown.
This Webservice you are using does not provide the search by address, but only by zip code. Has this site http://www.cepaberto.com/ which is also free that can help you because it allows various types of search
– Tiedt Tech
Yes @Marlontiedt, they even give an example - viacep.com.br/Ws/RS/Porto Alegre/Olavo/json/, but wanted to adapt the calls to the proper fields understand?
– João Victor Gomes Moreira
I hadn’t seen
– Tiedt Tech
+Favorite to see the API website after. P
– Inkeliz