Skip to content

Instantly share code, notes, and snippets.

@zerobugs-oficial
Created June 18, 2020 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zerobugs-oficial/44fbed8a12c0cdafdb3b28fb67c4d720 to your computer and use it in GitHub Desktop.
Save zerobugs-oficial/44fbed8a12c0cdafdb3b28fb67c4d720 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