0
Looking a little I found this method of consulting the zip code from neighborhood and city or all data from the zip code. But this query is only working when I put the information directly in the code between quotes. I’m not able to use a variable (that I enter the data passed by POST) inside $buscarcep->busca('[Aqui dentro!]')
.
The proleme is la at the bottom in // Show Routine
That is the code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento</title>
</head>
<body>
<?php
$rua01 = 'Rua diadema';
$cidade01 = 'Foz do Iguaçu';
$endereco01 = $rua01.' '.$cidade01;
class BuscaCEP
{
protected function formata($response)
{
$dom = new DOMDocument();
@$dom->loadHTML($response);
$xpath = new DOMXPath($dom);
$values = $xpath->query('//*[@class="respostadestaque"]');
$result = [];
// Se não encontrar CEP, retorna false
if (!$values->length) {
return false;
}
// Obtém informações desejadas, tratando-as quando necessário
foreach ($values as $value) {
$result[] = preg_replace(
'~[\s]{2,}~',
'',
trim($value->childNodes->item(0)->nodeValue)
);
}
list($logradouro, $bairro, $localidade, $cep) = $result;
list($localidade, $uf) = explode('/', $localidade);
return compact('logradouro', 'bairro', 'localidade', 'uf', 'cep');
}
public function busca($cep)
{
$response = file_get_contents(
'http://m.correios.com.br/movel/buscaCepConfirma.do',
false,
stream_context_create([
'http' => [
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query([
'cepEntrada' => $cep,
'metodo' => 'buscarCep',
]),
],
])
);
return $this->formata($response);
}
}
// Rotina de Exibir
$buscarcep = new BuscaCEP();
$dados = $buscarcep->busca($endereco01);
print_r($dados['bairro']);
?>
</body>
</html>
I already put in
$dados = $buscarcep->busca($enderecoP);
$dados = $buscarcep->busca("$enderecoP");
And just nothing came up. No error, just nothing printed.
Updating with more information, as they generated different forms of interpretation. There at the end of the code when I put:
// Rotina de Exibir
$buscarcep = new BuscaCEP();
$dados = $buscarcep->busca($endereco01);
print_r($dados['bairro']);
Within the variable $address01 I have $rua01. ' '.$city01, that if I were to echo her, I would be shown Rua diadema Foz do Iguaçu. But the way this one doesn’t recognize! Now if I take $address01 and put only 'Rua diadema Foz do Iguaçu' in this way:
// Rotina de Exibir
$buscarcep = new BuscaCEP();
$dados = $buscarcep->busca('Rua diadema Foz do Iguaçu');
print_r($dados['bairro']);
He recognizes perfectly and shows me the information of this address.
Here the output was Jardim Curitibano, works. Enable errors.
– Papa Charlie
What are the values of
$rua01
and$cidade01
? Shouldn’t be$ruaP
and$cidadeP
?– Woss
@Andersoncarloswoss yes, I ended up putting wrong, I’m using 01 at the end, but to pass here I decided to put P.
– dalton gonzalo Fuentes
And yet it doesn’t work? Try putting
var_dump($enderecoP)
before$buscarcep->busca(...)
– Woss
@Papacharlie is working pq if you repair this --- $data = $buscarcep->search('85869510'); --- Has the zip already inside, what I want to put is a variable in place of the number, because I will pass by POST this information.
– dalton gonzalo Fuentes
@Andersoncarloswoss not, as he told me he printed C: wamp64 www delivery2 test index.php:69:string 'Rua diadema Foz do Iguaçu' (length=26) In this case 'Rua diadema Foz do Iguaçu' are the data that I am going through by POST.
– dalton gonzalo Fuentes
Here worked perfectly the way you did, I didn’t change any line the problem should be elsewhere, has no relation to enter or not in
$buscarcep->busca(...)
, the variables POST inclusive are not sent to$buscarcep->busca();
, you are sending a'85869510'
. The problem for sure is the moment you pass the POST, or better not even be arriving in the POST. Show how you are doing the search for the address instead of zip code.– Guilherme Nascimento
@Guilhermenascimento I think I should have taken this '85869510', because they are misinterpreting me. The code like this, is working. But I need to put this zip code directly into the code. An example, if you say $cepConsult = '85869510'; and put $cepConsult into $buscarcep->search(inside), it just doesn’t recognize that the contents of that variable when it’s inside.
– dalton gonzalo Fuentes
@daltongonzaloFuentes already explained, the problem is not ae, you have to explain how you did, wait a little while I am formulating an answer that may help you.
– Guilherme Nascimento
@daltongonzaloFuents how are you calling this script? From a form submission?
– Woss
@Andersoncarloswoss right now I’m right in php, I took if(isset(...), IE, every time I update the page it running the Script
– dalton gonzalo Fuentes
But if you are not submitting a form, you will not have information in $_POST. Possibly about this William will respond. And that
if(isset(...))
is the one that’s not in the code?– Woss
@Guilhermenascimento It happens like this: if I put in the dots $buscarcep->search(...) '85869510', he looks for the information about this zip code, but if I put tbm directly in the dots 'Rua diadema Foz do Iguaçu' he also looks for the information about this address. Now if I put 'Rua diadema Foz do Iguaçu' inside a variable, $cepConsult for example, and put this variable inside the dots, it does not recognize. $buscarcep->busca('Rua diadema Foz do Iguaçu') -> Reconhece $cepConsulta = 'Rua diadema Foz do Iguaçu'; $buscarcep->busca($cepConsulta) -> Does not recognize
– dalton gonzalo Fuentes
@daltongonzaloFuentes Can you give us exactly the code you are testing when using the variable? You can play it on Pastebin, Ideone, whatever you want, and pass the link here.
– Woss
As I said, this with problem of handling errors and input data, I’m not being critical, I’m just saying, you are the programmer, PHP and even no language has to guess things if you as a programmer do not program for it to detect. ;)
– Guilherme Nascimento
@Andersoncarloswoss I updated the question with my code exactly like this one. Here is the link: https://ideone.com/fork/sXozcO I think this is correct.
– dalton gonzalo Fuentes