Skip to content

Instantly share code, notes, and snippets.

@papalardo
Last active February 3, 2019 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papalardo/5fb1006943b2afc25dd6ebfa6152a464 to your computer and use it in GitHub Desktop.
Save papalardo/5fb1006943b2afc25dd6ebfa6152a464 to your computer and use it in GitHub Desktop.
Listar estado com cidades do Brasil
$path = './api';
$filename = 'estados.json';
$filePath = $path .'/'. $filename;
if(file_exists($filePath)) {
$content = json_decode(file_get_contents($filePath), true);
return $content;
}
$somenteEssesEstados = ['RJ', 'SP'];
$estados = json_decode(file_get_contents('https://servicodados.ibge.gov.br/api/v1/localidades/estados'));
$estados = array_filter($estados, function($estado) use ($somenteEssesEstados) {
return $somenteEssesEstados ? in_array($estado->sigla, $somenteEssesEstados) : $estado;
});
$estados = array_map(function($estado) {
$estado->cidades = json_decode(file_get_contents("https://servicodados.ibge.gov.br/api/v1/localidades/estados/{$estado->id}/microrregioes"));
return $estado;
}, $estados);
if(!file_exists($path))
mkdir($path);
if(!file_exists($filePath)) {
file_put_contents($filePath, json_encode($estados));
}
return $estados;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment