Forked from jonatanfroes/gist:1935123
Created
August 2, 2016 06:43
Star
You must be signed in to star a gist
Buscar CEP CodeIgniter
This file contains 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
//helper cep | |
if ( ! function_exists('buscar_endereco')) | |
{ | |
function buscar_endereco($cep) | |
{ | |
$cep = str_replace('.', '', $cep); | |
$cep = str_replace('-', '', $cep); | |
$url = 'http://republicavirtual.com.br/web_cep.php?cep='.urlencode($cep).'&formato=query_string'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_VERBOSE, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); | |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | |
curl_setopt($ch, CURLOPT_POST, 0); | |
$resultado = curl_exec($ch); | |
curl_close($ch); | |
if( ! $resultado) | |
$resultado = "&resultado=0&resultado_txt=erro+ao+buscar+cep"; | |
$resultado = urldecode($resultado); | |
$resultado = utf8_encode($resultado); | |
parse_str( $resultado, $retorno); | |
return json_encode($retorno); | |
} | |
} | |
//controller ajax | |
public function buscar_endereco() | |
{ | |
$cep = $this->input->get_post('cep'); | |
if( strstr($cep, '_') || strlen($cep) < 8 ) | |
{ | |
$cep = '0'; | |
} | |
$this->load->helper('correios'); | |
$this->output->set_output( buscar_endereco($cep) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment