Skip to content

Instantly share code, notes, and snippets.

@rederlo
Last active December 14, 2015 19:08
Show Gist options
  • Save rederlo/75b1dc98d2f635a540bf to your computer and use it in GitHub Desktop.
Save rederlo/75b1dc98d2f635a540bf to your computer and use it in GitHub Desktop.
Cakephp Componet consulta cep
<?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' => '']);
}
}
}
@rederlo
Copy link
Author

rederlo commented Dec 14, 2015

Exemplo de component compatível com cake 2.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment