Skip to content

Instantly share code, notes, and snippets.

@paseto
Last active February 25, 2016 19:54
Show Gist options
  • Save paseto/870f78dcfc16f3c7d652 to your computer and use it in GitHub Desktop.
Save paseto/870f78dcfc16f3c7d652 to your computer and use it in GitHub Desktop.
Busca CEP Correios 2016 - PHP
$fo = fopen("cep.html", "w+");
$url = "http://wxww.buscacep.correios.com.br/sistemas/buscacep/resultadoBuscaEndereco.cfm";
$cep = "03712000";
set_time_limit(0);
$process = curl_init(str_replace(" ", "%20", $url));
curl_setopt($process, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded'));
curl_setopt($process, CURLOPT_POST, TRUE);
curl_setopt($process, CURLOPT_POSTFIELDS, "CEP=" . $cep . "");
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
fwrite($fo, $return);
$dom = new DOMDocument;
$dom->loadHTMLFile('cep.html');
$cells = $dom->getElementsByTagName('td');
foreach ($cells as $cell) {
echo $cell->nodeValue, PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment