-
-
Save GustavoMartinsSantos/491351eb5504a791c1773f010db6d55c to your computer and use it in GitHub Desktop.
Como pegar endereço completo através do CEP usando PHP
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 | |
function get_endereco($cep){ | |
// formatar o cep removendo caracteres nao numericos | |
$cep = preg_replace("/[^0-9]/", "", $cep); | |
$url = "http://viacep.com.br/ws/$cep/xml/"; | |
$xml = simplexml_load_file($url); | |
return $xml; | |
} | |
?> | |
<meta charset="utf-8"> | |
<h1>Pesquisar Endereço</h1> | |
<form action="" method="post"> | |
<input type="text" name="cep"> | |
<button type="submit">Pesquisar Endereço</button> | |
</form> | |
<?php if($_POST['cep']){ ?> | |
<h2>Resultado da Pesquisa</h2> | |
<p> | |
<?php $endereco = get_endereco("37500405"); ?> | |
<b>CEP: </b> <?php echo $endereco->cep; ?><br> | |
<b>Logradouro: </b> <?php echo $endereco->logradouro; ?><br> | |
<b>Bairro: </b> <?php echo $endereco->bairro; ?><br> | |
<b>Localidade: </b> <?php echo $endereco->localidade; ?><br> | |
<b>UF: </b> <?php echo $endereco->uf; ?><br> | |
</p> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment