Skip to content

Instantly share code, notes, and snippets.

@paulofreitas
Last active December 20, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulofreitas/6068344 to your computer and use it in GitHub Desktop.
Save paulofreitas/6068344 to your computer and use it in GitHub Desktop.
Busca de CEP através do site dos Correios
<?php
/*
* Busca de CEP através do site dos Correios
*/
function buscaCEP($cep) {
$dom = new DOMDocument();
$dom->loadHTMLFile(
"http://www.correios.com.br/encomendas/malote/endereco.cfm?tipo=origem&cep=$cep");
$xpath = new DOMXPath($dom);
$data = $xpath->query('//input');
$aux = $data->item(2)->getAttribute('value');
if (!empty($aux)) {
$aux = array_map('trim', explode('/', $aux));
return (object) array(
'endereco' => $data->item(0)->getAttribute('value'),
'bairro' => $data->item(1)->getAttribute('value'),
'cidade' => $aux[0],
'uf' => $aux[1]
);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment