Skip to content

Instantly share code, notes, and snippets.

@hannahlere
Forked from GustavoMartinsSantos/get_address.php
Created April 13, 2023 16:54
Show Gist options
  • Select an option

  • Save hannahlere/4887417d267c5404abb418f6f9d8679d to your computer and use it in GitHub Desktop.

Select an option

Save hannahlere/4887417d267c5404abb418f6f9d8679d 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