-
-
Save fayqLs/0b76d9ae13f41183ee69ed1f66800479 to your computer and use it in GitHub Desktop.
BUSCAR O ENDEREÇO PELO CEP
This file contains hidden or 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 | |
ini_set('display_errors',0); | |
class PessoaForm extends TPage | |
{ | |
public static function onCEP($param = null) | |
{ | |
try | |
{ | |
if (isset($param['cep']) AND !empty($param['cep'])) | |
{ | |
$cep = str_replace(['.','-',' '],['','',''], $param['cep']); | |
$consulta = file_get_contents("https://brasilapi.com.br/api/cep/v1/{$cep}"); | |
$consulta = json_decode($consulta); | |
if (json_last_error() !== JSON_ERROR_NONE) | |
{ | |
TToast::show("warning", "ENDEREÇO NÃO ENCONTRADO", "center", "fas:exclamation"); | |
return; | |
} | |
if (isset($consulta->state) and !empty($consulta->state)) | |
{ | |
$objeto = new stdClass(); | |
$objeto->endereco = trim($consulta->street); | |
$objeto->bairro = trim($consulta->neighborhood); | |
$objeto->cidade = trim($consulta->city); | |
$objeto->uf = trim($consulta->state); | |
} | |
# PREENCHER A COMBO CIDADE | |
$city = trim($consulta->city); | |
if ($city) | |
{ | |
TTransaction::open(self::$database); | |
$cidade = Cidade::where('nome','=', $city)->first(); | |
if ($cidade) {$objeto->cidade_id = $cidade->id;} | |
TTransaction::close(); | |
} | |
TForm::sendData(self::$formName, $objeto); | |
TCombo::reload(self::$formName, 'estado_id', Estado::getIndexedArray('id', 'nome'), true); | |
TCombo::reload(self::$formName, 'cidade_id', Cidade::getIndexedArray('id', 'nome'), true); | |
} | |
} | |
catch (Exception $e) | |
{ | |
new TMessage('error', $e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment