Hello,
Only the zip code search to find the city and state works, but the city and state search to find the ZIP code does not work because in JSON, the zip code (% with%) is inside the keys of the numeric class and there is no state-city class.
Here's PHP:
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
function webClient($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$descubra = $_POST['descubra'];
switch ($descubra)
{
case "lugar":
$cidade = $_POST['cidade'];
$estado = $_POST['estado'];
$bairro = $_POST['bairro'];
$url = sprintf('https://viacep.com.br/ws/%s/%s/%s/json/ ', $estado, $cidade, $bairro);
$result = json_decode(webClient($url));
echo $result->cep;
break;
case "ceplocal":
$cep = $_POST['cep'];
$url = sprintf('https://viacep.com.br/ws/%s/json/ ', $cep);
$result = json_decode(webClient($url));
echo $result->localidade;
echo $result->uf;
break;
default:
echo "Inválido!";
}
}