Last active
December 14, 2015 19:08
-
-
Save rederlo/75b1dc98d2f635a540bf to your computer and use it in GitHub Desktop.
Cakephp Componet consulta cep
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
<?php | |
App::uses('Component', 'Controller'); | |
/** | |
* Created by PhpStorm. | |
* User: Ederlo Rodrigo de Oliveira | |
* Date: 14/12/15 | |
* Time: 09:49 | |
*/ | |
class LocateMeComponent extends Component | |
{ | |
public $url = 'http://cep.correiocontrol.com.br'; | |
public $url2 = 'https://viacep.com.br/ws'; | |
public $type = 'json'; | |
/** | |
* @param int $cep | |
* @return mixed | |
*/ | |
public function cep($cep = 0) | |
{ | |
try{ | |
$cep = filter_var($cep, FILTER_SANITIZE_NUMBER_INT); | |
$json = file_get_contents("{$this->url2}/{$cep}/{$this->type}"); | |
return json_decode($json, true); | |
} catch (Exception $e){ | |
$cep = filter_var($cep, FILTER_SANITIZE_NUMBER_INT); | |
$json = file_get_contents("{$this->url}/{$cep}.{$this->type}"); | |
return json_decode($json, true); | |
} catch (Exception $e){ | |
return json_decode(['json' => '']); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exemplo de component compatível com cake 2.x