Skip to content

Instantly share code, notes, and snippets.

@GustavoMartinsSantos
Forked from zerobugs-oficial/get_address.php
Created December 6, 2020 03:36
Show Gist options
  • Save GustavoMartinsSantos/491351eb5504a791c1773f010db6d55c to your computer and use it in GitHub Desktop.
Save GustavoMartinsSantos/491351eb5504a791c1773f010db6d55c to your computer and use it in GitHub Desktop.
Como pegar endereço completo através do CEP usando PHP
<?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