Error in PHP zip code query

Asked

Viewed 167 times

0

I am trying to fix in PHP the URL that queries the ZIP code, when typing the ZIP CODE XXXXX-XXX the URL is displaying the characters amp; that are causing error in XML output.

Correct consultation:

http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=88160-282

Query with error:

http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=88160-282

How to remove amp; characters from query?

    // FUNÇAO PARA CONSULTAR CEP

public function consultarCep() {
    $cep = $_POST['cep'];

    $reg = simplexml_load_file("http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=" . $cep);

    $dados['sucesso'] = (string) $reg->resultado;
    $dados['endereco'] = (string) $reg->tipo_logradouro . ' ' . $reg->logradouro;
    $dados['bairro'] = (string) $reg->bairro;
    $dados['cidade'] = (string) $reg->cidade;
    $dados['estado'] = (string) $reg->uf;

    echo json_encode($dados);
}
  • // FUNCTION TO CONSULT CEP public Function consultrCep() { $cep = $_POST['cep']; $reg = simplexml_load_file("http://cep.republicavirtual.com.br/web_cep.php?formatxml&cep=" . $cep); $data['success'] = (string) $reg->result; $data['address'] = (string) $reg->type_street . ' ' $reg->street; $data['neighborhood'] = (string) $reg->neighborhood; $data['city'] = (string) $reg->city; $data['state'] = (string) $reg->Uf; echo json_encode($data); }

  • Click [Edit] to add to the above question, and use the Control K shortcut to format as code (or use the button { } from the format bar)

  • Updated question.

  • Not related to the &amp problem, but it is not better to take JSON anyway?

  • I will try to change the query. Thank you!

  • simplexml_load_file does some crazy things even in the exhaust (before version 5.1 of PHP was different, and depends on the version of Libxml). So, JSON is much simpler pro PHP, and you can pick the string with file operation, with Curl, has many ways (for example, $json = file_get_contents( caminho ); $data = json_decode($json, true);

Show 1 more comment

1 answer

-1


You can use the function html_entity_decode.

Example:

$var = "http://cep.republicavirtual.com.br/web_cep.php?formato=xml&cep=88160-282";

echo html_entity_decode($var);
  • This is the answer to your question, but I don’t think it solves the error in the XML result, if the error persists ask another question displaying the error.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.