CNPJ query using PHP

Asked

Viewed 2,095 times

2

The situation is the following, I have a script in PHP that makes CNPJ queries by the site of Receita Federal, but it is returning me some errors, I don’t really know if they’re mistakes.

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

Like the code of funcoes.php is too big and would disturb the question by leaving it too extensive, I will 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 don’t know if I’m right, but the result should be returned the same way it’s returned to the IRS. Excuse my ignorance in the subject and in the PHP language, but if this script is not wrong, how can I format this result?

Actually, for anyone who wants to see it 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 reason}. This is just an example, summarizing, I need to format the result to understand it.

Note: I know that questions made to link base can and probably will receive downvotes, but I want to make it clear that my intention is good, since I am also providing a very good script of functional cnpj queries and do not intend to remove the link, unless that is removed by some third party

  • What’s the problem? What are the mistakes?

  • The "error" is in the result, I edited the question with what I want. @rray, I don’t know for sure, if this is exactly a mistake or the right result, but what I need is for the result to be format, or at least a guide for me to format myself. I could tell?

  • 1

    echo $campos[0] .' - '. $campos[4]; that?

  • Yes that, the part about array(23) { [0]=> string(18) "17.81X.03X/0X01-XX"

  • 1

    that ai the result of var_dump to get the values individually you need to do it manually as I put in the previous comment.

  • sorry @rray but, how could I do this?

  • vc need to put the desired Index(number) in the variable campos for example the 1 opinion the cnpj the 2 the company type and the 3 the name, to display them first remove the var_dump($campos) and put echo $campos[0] .' # '. $campos[2] . ' # '. $campos[3];

  • To make it easier to read this information in the future, use this: echo '<pre>'; print_r($campos);

  • Friend, if it’s not too much, could you help me by way of answer? It would help too much and I believe that other people might have the same problem that I and with your help would solve theirs too

  • Was that the problem? can I answer yes

  • It was @rray, I just need the results to appear in a way that I can understand correctly and so I can include in a layout later. I appreciate the help

Show 6 more comments

2 answers

3


It’s no mistake that’s just the way out of var_dump($campos), to manipulate or display the information individually it is necessary to enter the desired index.

echo 'CNPJ: '. $campos[0] . ' Nome da empresa: '. $campos[3];
  • In the end it was very simple, thank you @rray thank you for your help

  • And when you change the array, how can I select another array ? For example: [5]=> array(2) { [0]=> string(67) "47.X1-X-02 - Recarga de XXX para equipamentos de XXXXX " [1]=> string(80)

  • In this case, the array is the value (2) as I select it to be able to pull the $fields[0] of array 2?

  • 1

    @Victorgomes you need to do echo $campos[5][0] pq there is an array inside the index 5. anything warns there

  • Returned the value: Array[0], did not work

  • can check out Here the results, I’m still tidying up some things, I tried to do as close to the IRS itself, but anyway, I think it’s best for you to see testing

  • 1

    @Victorgomes do so and shows the result, echo '<pre>' print_r($campos[5]);

  • '' print_r(Array) now returned that error

  • @Victorgomes later I will take a look at the example.

  • All right, thank you @rray

  • I’m cracking my head and I’m not getting it, sorry for the ignorance in the subject, but it’s complicated haha

  • @Victorgomes I can not access from here the link, by the question code the right is echo $campos[5][0] or echo $campos[5][0] pq inside the key 5 have another array, then you access the key 0 or 1 .

  • so I think I’m going to get stuck on this part, I’m going to take a look and hopefully I can sort it out soon, because basically that’s all that’s missing, obg @rray I appreciate the patience

Show 8 more comments

2

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

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

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

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

To show row by row of the array you could mount a loop using for example the loop for:

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

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

In the above example I use the "Count()" to know how many items the array has to do the for.

Well I guess that’s it

Att;

  • 1

    Well explained, I understood that it was not a mistake in the code but my haha error, I thank @Fleuquer-file

  • I’m glad I can help, anything I can do!

  • in the case of parts of the ARRAY, how can I select them ? For example to select the string is $fields[4] and to select the field 0 of the array 2 ?

  • For example, in one of the examples you posted one of the indexes only your array is: "X7.X1-X-01 - XXX Specialized Retail". This is for example the "$field[3]". And all this is a String (text basically), if you want it to return only the word "Trade" for example, you would have to break the string either to another array or to a position within the string. But only employee for all your cases if the positions of the breaks are equal or marked by a "space" or a special character type "/".

  • I managed to solve, thank you very much my friend, it was really worth

Browser other questions tagged

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