Zip Code Search in PHP / HTML [closed]

-4

What is missing for this code to bring the zip code result?

<!DOCTYPE html>
<!--<! -- Trazer Resultados CEP, Rua, Bairro e Estado -->

<html lang="pt-br"> <!-- Declaração do Idioma  -->
    <head>
        <meta charset="UTF-8">
        <title> MEU CEP </title>
    <label> Insira o CEP: </label>
    <input type="text" name="cep"> 
    <input type="submit" value="Enviar"> <br />
    <label>Rua:
        <input name="rua" type="text" id="rua" size="60" /></label><br />
    <label>Bairro:
        <input name="bairro" type="text" id="bairro" size="40" /></label><br />
    <label>Estado:
        <input name="uf" type="text" id="uf" size="2" /></label><br />

    <?php
    if (!empty($_POST['cep'])) {

        $cep = $_POST['cep'];

        $address = (get_address($cp));

        echo "<br><br>CEP Informado: $cep<br>";
        echo "Rua: $addres->logradoro<br>";
        echo "Bairro: $address->bairro<br>";
        echo "Estado: $adress->uf<br>";
    }

    function get_address($cep) {


        $cep = preg_replace("/[^0-9]/", "", $cep);
        $url = "http://viacep.com.br/ws$cep/xml/";

        $xml = simplexml_load_file($url);
        return $xml;
    }
    ?>
</body>
</html>

I made a new version with fixes and inserted the class Address, but it is not yet returning to Street, Neighborhood and State

<!DOCTYPE html>
<!--<! -- Trazer Resultados CEP, Rua, Bairro e Estado -->

<html lang="pt-br"> <!-- Declaração do Idioma  -->
    <head>
        <meta charset="UTF-8">
        <title> MEU CEP </title>
    <label> Insira o CEP: </label>
    <input type="text" name="cep"> 
    <input type="submit" value="Enviar"> <br />
    <label>Rua:
        <input name="rua" type="text" id="rua" size="60" /></label><br />
    <label>Bairro:
        <input name="bairro" type="text" id="bairro" size="40" /></label><br />
    <label>Estado:
        <input name="uf" type="text" id="uf" size="2" /></label><br />

    <?php
    if (!empty($_POST['cep'])) {

        $cep = $_POST['cep'];

        $address = (get_address($cep));

        echo "<br><br>CEP Informado: $cep <br>";
        echo "Rua: $addres->logradoro<br>";
        echo "Bairro: $address->bairro<br>";
        echo "Estado: $address->uf<br>";
    }

    class Address {

        public $bairro;
        public $logradouro;
        public $uf;

        function get_address($cep) {


            $url = "http://viacep.com.br/ws/" . $cep . "/xml";
            $xml = simplexml_load_file($url);
            $this->bairro = $xml->bairro;
            $this->logradouro = $xml->logradouro;
            $this->uf = $xml->uf;
        }

    }
    ?>
</body>
</html>
    
asked by anonymous 12.12.2018 / 01:11

2 answers

1

Your code has several typos.

  

It's not $ cp

$address = (get_address($cp));
  

and yes $ zip

$address = (get_address($cep));
  

is not $ addres nor $ adress

echo "Rua: $addres->logradoro<br>";
echo "Estado: $adress->uf<br>";
  

and yes $ address

echo "Rua: $address->logradoro<br>";
echo "Estado: $address->uf<br>";
  

Not a logger

echo "Rua: $address->logradoro<br>";
  

and it's a street instead

echo "Rua: $address->logradouro<br>";

The address http://viacep.com.br/ws$cep/xml/ is wrong,

Correct is http://viacep.com.br/ws/$cep/xml/

Where is the <form>...</form> tag for your form?

Use this code that will work: functional example >

<?php
function get_endereco($cep){
  // formatar o cep removendo caracteres nao numericos
  $cep = preg_replace("/[^0-9]/", "", $cep);
  $url = "http://viacep.com.br/ws/$cep/xml/";
  $xml = simplexml_load_file($url);
  return $xml;
}
?>
<meta charset="UTF-8">
<h1>Pesquisar Endereço</h1>
<form action="" method="post">
  <input type="text" name="cep">
  <button type="submit">Pesquisar Endere&ccedil;o</button>
</form>


<?php if($_POST['cep']){ ?>
<h2>Resultado da Pesquisa</h2>
<p>
  <?php $endereco = get_endereco($_POST['cep']); ?>
  <b>CEP: </b> <?php echo $endereco->cep; ?><br>
  <b>Logradouro: </b> <?php echo $endereco->logradouro; ?><br>
  <b>Bairro: </b> <?php echo $endereco->bairro; ?><br>
  <b>Localidade: </b> <?php echo $endereco->localidade; ?><br>
  <b>UF: </b> <?php echo $endereco->uf; ?><br>
</p>
<?php } ?>

OR IF YOU PREFER TO USE YOUR CORRECT

Functional sample

<!DOCTYPE html>
<!--<! -- Trazer Resultados CEP, Rua, Bairro e Estado -->

<html lang="pt-br"> <!-- Declaração do Idioma  -->
    <head>
        <meta charset="UTF-8">
        <title> MEU CEP </title>

<form action="" method="post">        
    <label> Insira o CEP: </label>
    <input type="text" name="cep"> 
    <input type="submit" value="Enviar"> <br />
    <label>Rua:
        <input name="rua" type="text" id="rua" size="60" /></label><br />
    <label>Bairro:
        <input name="bairro" type="text" id="bairro" size="40" /></label><br />
    <label>Estado:
        <input name="uf" type="text" id="uf" size="2" /></label><br />
</form>
    <?php
    if (!empty($_POST['cep'])) {

        $cep = $_POST['cep'];

        $address = (get_address($cep));

        echo "<br><br>CEP Informado: $cep<br>";
        echo "Rua: $address->logradouro<br>";
        echo "Bairro: $address->bairro<br>";
        echo "Estado: $address->uf<br>";
    }

    function get_address($cep) {


        $cep = preg_replace("/[^0-9]/", "", $cep);
        $url = "http://viacep.com.br/ws/$cep/xml/";

        $xml = simplexml_load_file($url);
        return $xml;
    }
    ?>
</body>
</html>
    
12.12.2018 / 02:42
-2

missing only include the slash before the zip number, the syntax is: link

$url = 'http://viacep.com.br/ws/'.$cep.'/xml/';
    
12.12.2018 / 01:46