Last active
December 20, 2015 04:09
-
-
Save paulofreitas/6068344 to your computer and use it in GitHub Desktop.
Busca de CEP através do site dos Correios
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 | |
/* | |
* 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