Skip to content

Instantly share code, notes, and snippets.

@mvnp
Forked from jonatanfroes/gist:1935123
Created August 2, 2016 06:43
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mvnp/31d7631bddb93d0415259a7eaf222311 to your computer and use it in GitHub Desktop.
Buscar CEP CodeIgniter
//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