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); }
– aleestevao
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)– Bacco
Updated question.
– aleestevao
Not related to the & problem, but it is not better to take JSON anyway?
– Bacco
I will try to change the query. Thank you!
– aleestevao
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);
– Bacco