Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Gilberto Albino
- * @description Baseado em
- * http://www.phpclasses.org/package/7728-PHP-Get-the-address-from-a-CEP-zip-code-in-Brazil.html
- *
- */
- class Correios
- {
- public function getEnderecoByCep( $cep )
- {
- $ch = curl_init();
- $url = "http://www.buscacep.correios.com.br/"
- . "servicos/dnec/consultaEnderecoAction.do"
- ;
- curl_setopt_array( $ch,
- array (
- CURLOPT_URL => $url,
- CURLOPT_POST => TRUE,
- CURLOPT_POSTFIELDS => "relaxation={$cep}"
- . "&TipoCep=ALL"
- . "&semelhante=N"
- . "&Metodo=listaLogradouro"
- . "&TipoConsulta=relaxation"
- . "&StartRow=1"
- . "&EndRow=10"
- . "&cfm=1",
- CURLOPT_RETURNTRANSFER => TRUE
- )
- );
- $response = curl_exec( $ch );
- curl_close( $ch );
- preg_match_all( "/>(.*?)<\/td>/", $response, $matches );
- $dados = $matches[1];
- /**
- * Retornará nullo || Will return null
- */
- $obj = new stdClass();
- $obj->logradouro = isset( $dados[0] ) ? $dados[0] : null;
- $obj->bairro = isset( $dados[1] ) ? $dados[1] : null;
- $obj->localidade = isset( $dados[2] ) ? $dados[2] : null;
- $obj->uf = isset( $dados[3] ) ? $dados[3] : null;
- $obj->cep = isset( $dados[4] ) ? $dados[4] : null;
- return $obj;
- }
- }
- /**
- * Uso simples || Simple usage
- */
- $correios = new Correios();
- $info = $correios->getEnderecoByCep( '01046-000' );
- echo $info->logradouro;
- /**
- * Debug simples em caso de erro || Simple debug if fault
- */
- $info = $correios->getEnderecoByCep( '12121-121' );
- var_dump($info->logradouro);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement