CNPJ query using PHP

2

The situation is as follows, I have a script in PHP that does CNPJ queries through the IRS website, however it is returning some errors, I really do not know if they are errors .

HTML / PHP code

consulta.html

    <html>
    <head>
            <title>CNPJ e Captcha</title>
    </head>
    <body>
    <form method="post" action="processa.php">
            <p><span class="titleCats">CNPJ e Captcha</span>
              <br />
              <input type="text" name="CNPJ" maxlength="19" required /> 
              <b style="color: red">CNPJ</b>
              <br />
              <img src="getcaptcha.php" border="0">
              <br />
              <input type="text" name="CAPTCHA" maxlength="6" required />
              <b style="color: red">O que vê na imagem acima?</b>
              <br />
            </p>
            <p>
              <input id="id_submit" name="enviar" type="submit" value="Consultar"/>
            </p>
    </form>
    </body>
    </html>

processa.php

    <?php

    require('funcoes.php');

    $cnpj = $_POST['CNPJ'];
    $captcha = $_POST['CAPTCHA'];

    // pega html resposta da receita
    $getHtmlCNPJ = getHtmlCNPJ($cnpj, $captcha);

    if($getHtmlCNPJ)
    {
    // volova os dados em um array
    $campos = parseHtmlCNPJ($getHtmlCNPJ);
    var_dump($campos);
    }
    ?>

funcoes.php

Since the code of funcoes.php is too large and would deconfigure the question by leaving it too long, I'll leave the link with the whole code, to see just click here

Now the result:

  array(23) {
     [0]=> string(18) "17.81X.03X/0X01-XX"
     [1]=> string(10) "13/03/2013"
     [2]=> string(58) "XXXXXXX - EMPRESA - ME"
     [3]=> string(10) "TONER XXXXX"
     [4]=> string(90) "X7.X1-X-01 - Comércio varejista especializado 
  de XXX"
     [5]=> array(2) {
        [0]=> string(67) "47.X1-X-02 - 
  Recarga de XXX para equipamentos de XXXXX "
        [1]=> string(80) 
  "95.11-8-00 -Reparação e XXX de XX e de XXXXX XX"
     }
     [6]=> string(68) "230-5 - XXXXXXXXXXXXXX (DE NATUREZA EMPRESARIA)" 
     [7]=> string(37) "R GERONIMO DOS SANTOS (JD W XX)"
     [8]=> string(2) "55"
     [9]=> string(0) ""
     [10]=> string(10) "09.X70-XXX"
     [11]=> string(15) "NOVA XXXX"
     [12]=> string(21) "SAO XXXXXX XXXX"
     [13]=> string(2) "SP"
     [14]=> string(26) "assessoriaprisma@XXX"
     [15]=> string(14) "(11) 4X5-3XX08" 
     [16]=> string(5) "*****"
     [17]=> string(5) "ATIVA"
     [18]=> string(10) "13/03/2XXX"
     [19]=> string(0) ""
     [20]=> string(8) "********"
     [21]=> string(8) "********" 
     ["status"]=> string(2) "OK"
 }

I do not know if I'm right, but the result should be returned the same way it is returned in the federal revenue. Sorry for my ignorance in the subject and in the PHP language, but if this script is not wrong, how can I do it to format this result?

Alias, for those who want to see live, try it yourself

What do you want?

I need the result to return formatted. How so? For example, a line indicating CNPJ {cnpj} another line indicating SOCIAL REASON {social name}. This is just an example, in short, I need to format the result for the understanding of it.

  

Note: I know that link-based questions can and probably will get downvotes, but I want to make it clear that my intent is   good, since I'm also making a very good   functional cnpj queries and I do not intend to remove the link, unless   that is removed by some third party

    
asked by anonymous 06.04.2016 / 13:48

2 answers

3

It's no mistake that this is just the output of var_dump($campos) , to manipulate or display the information individually you need to enter the desired index.

echo 'CNPJ: '. $campos[0] . ' Nome da empresa: '. $campos[3];
    
06.04.2016 / 14:17
2

The result is showing up because you are using " var_dump ". This function will show a structured representation of one or more expressions, including type and value. In your case your array looks normal, but to show it the way you want you can either use " print_r " or " echo ", like this:

// indicando sempre o índice do dado que deseja
print_r($campos[0]);
echo $campos[0];

If you use " print_r " you can put it without the index, but this would show the entire array.

//assim mostra todo array e suas chaves.
print_r($campos);

In order to show row by row of the array you could mount a loop using eg the for loop:

$campos = array('0' => "Pimeiro", '2' => "Segundo", '3' => "Terceiro" );
$itens = count($campos);

for ($i=0; $i <= $itens; $i++) { 
    echo ($campos[$i]."<br>");
}

In the example above I use " count () " to know how many items the array has in order to do it.

Well I guess that's it

Att;

    
06.04.2016 / 14:22