Search Postcode Post

3

I'm trying to implement a php implementation via simplexml_load_file to check and return data to the Post API. In this case, it is necessary to pass variables in url . I'm doing this:

I'm doing this:

function encontraCep() {
      $cep = $_POST["txtCep"];

      $url = "https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl&txtCep=".$cep;      
      $xml = simplexml_load_file($url);


      return $xml;

  }

 dados = encontraCep();

A simple

 print "<pre>";
 print_r($dados);
 print "</pre>";

Return me:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [name] => AtendeClienteService
            [targetNamespace] => http://cliente.bean.master.sigep.bsb.correios.com.br/
        )

)

Where am I going wrong?

I am following the tutorial given in link , done in asp.net

There he makes a form:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
    <h1>Teste WS dos Correios</h1><br />
    <asp:Panel ID="Panel1" runat="server" GroupingText="Busca Endereço">
      CEP:
      <asp:TextBox ID="txtCep" runat="server"></asp:TextBox>&amp;nbsp;<asp:Button ID="btnBuscarEndereco" runat="server" OnClick="btnBuscarEndereco_Click" Text="Buscar Endereço" />
      <br />
      <asp:Label ID="lblEndereco" runat="server"></asp:Label>
    </asp:Panel>
    </div>
  </form>
</body>
</html>

And a function that takes the return:

protected void btnBuscarEndereco_Click(object sender, EventArgs e)
{
  wsCorreio.AtendeClienteClient ws = new wsCorreio.AtendeClienteClient("AtendeClientePort"); //Verificar o nome do endpoint no arquivo Web.config
  var dados = ws.consultaCEP(txtCep.Text);
  if (dados != null)
  {
    lblEndereco.Text = string.Format(@"Endereço: {0}<br />
                       Complemento 1: {1}<br />
                       Complemento 2: {2}<br />
                       Bairro: {3}<br />
                       Cidade: {4}<br />
                       Estado: {5}",
                       dados.end,
                       dados.complemento,
                       dados.complemento2,
                       dados.bairro,
                       dados.cidade,
                       dados.uf);
  }
  else
    lblEndereco.Text = "CEP não encontrado.";
}

But there is no action in form of it to see the sending of url .

    
asked by anonymous 25.05.2016 / 14:41

3 answers

5

You must use SoapClient to load the #

In the code below you retrieve the information using the zip code number.

<?php   

    $config = array(
        "trace" => 1, 
        "exception" => 0, 
        "cache_wsdl" => WSDL_CACHE_MEMORY
    );

    $address = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl';   

    $client = new SoapClient($address, $config);

    $cep  = $client->consultaCEP(['cep' =>'01415000']);

Result

object(stdClass)#2 (1) { ["return"]=> object(stdClass)#3 (8) 
{ ["bairro"]=> string(12) "Consolação" ["cep"]=> string(8) "01415000"
  ["cidade"]=> string(10) "São Paulo" ["complemento"]=> string(0) "" 
  ["complemento2"]=> string(21) "- até 586 - lado par" 
  ["end"]=> string(15) "Rua Bela Cintra" ["id"]=> int(0) 
  ["uf"]=> string(2) "SP" } 
}

Sequence:

$config: configurações do serviço

$address: endereço do WSDL

$client: instância da classe SoapClient com as configurações e endereço

$cliente->consultaCEP: é a função que recupera as informações do CEP informado

Notes:

1) In the CEP query to function is an Array in the format [cep => 'numero do cep']

2) This WebService is too slow!

Invalid zip and Errors:

Use this:

try
{
    $cep  = $client->consultaCEP(['cep' =>'11111111']);
    var_dump($cep);
}
catch(Exception $e) 
{
    var_dump($e);
}
  

object (SoapFault) # 3 (10) {["message": protected] = > string (18) "Zip Code   FOUND "[" string ":" Exception ": private] => string (0)" "   ["code": protected] = > int (0) ["file": protected] = > string (26)   "C: \ inetpub \ wwwroot \ cep.php" ["line": protected] = > int (16)   ["trace": "Exception": private] = > array (1) {[0] = > array (6) {["file"] = >   string (26) "C: \ inetpub \ wwwroot \ cep.php" ["line"] = > int (16)   ["function"] = > string (6) "__call" ["class"] = > string (10) "SoapClient"   ["type"] = > string (2) "- >" ["args"] = > array (2) {[0] = > string (11)   "queryCEP" 1 = > array (1) {[0] = > array (1) {["cep"] = > string (8)   "11111111"}}}}} ["previous": "Exception": private] = > NULL   ["faultstring"] = > string (18) "Zip Code Not Found" ["faultcode"] = >   string (11) "soap: Server" ["detail"] = > object (stdClass) # 2 (1) {   ["SigepClientException"] = > string (18) "ZIP CODE NOT FOUND"}}

You can then check if you have not returned anything!

Ready PHP packages on the site PackagistThe PHP Package Repository

1 - zizaco / cep-consult through the post office website

2 - canducci / cep by viacep.com.br site

3 - cagartner / postal-consultation has even freight calculation

    
26.05.2016 / 02:07
2

You have an example in PHP for this, including I've already used it, go to link . In Url that uses in your example does not return anything, it uses the url link which you can even test in your browser that works, just pass the zip.

As you can see in the example, just call the url and work with the return:

<?php

$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['rua']     = (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);

?>
    
25.05.2016 / 20:27
0

Another way is through the viacep.com.br site

Code:

<?php   

    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;
    }
    $cep = "01414000";
    $url = sprintf('http://viacep.com.br/ws/%s/json/ ', $cep);
    $result = json_decode(webClient($url));

    var_dump($result);

Result:

object(stdClass)#1 (9) { 
 ["cep"]=> string(9) "01414-000" 
 ["logradouro"]=> string(16) "Rua Haddock Lobo" 
 ["complemento"]=> string(20) "até 1048 - lado par" 
 ["bairro"]=> string(16) "Cerqueira César" 
 ["localidade"]=> string(10) "São Paulo" 
 ["uf"]=> string(2) "SP" 
 ["unidade"]=> string(0) "" 
 ["ibge"]=> string(7) "3550308" 
 ["gia"]=> string(4) "1004" }

How to use:

echo $result->cep;
echo $result->logradouro;
echo $result->complemento;
echo $result->bairro;
echo $result->localidade;
echo $result->uf;
echo $result->unidade;
echo $result->ibge;
echo $result->gia;
    
28.05.2016 / 20:29