Login to the NFe portal with cURL and PHP

5

I've done a lot of research on this and so far I have not been able to do anything concrete. Is this possible?

I want to make a form with the access key field and the captcha code to fill out. soon this data will be sent to the nfe portal and will return the nfe information to me.

This causes the expired session error.

index.php

 

                          Untitled Document      

<body> 
    <?php

    function recebe_imagem($url, $arquivo, $cookief = "", $cookiej = "") {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        /* if(!empty($cookief)) { 
          curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
          }
          if(!empty($cookiej)) {
          curl_setopt($ch, CURLOPT_COOKIESESSION, "cookie.txt");
          } */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIESESSION, "cookie.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0)");

        //curl_setopt($ch, , 
        $data = curl_exec($ch);
        //curl_close ($ch); 
        $fp = fopen($arquivo, 'w');
        fwrite($fp, $data);
        fclose($fp);
        return $arquivo;
    }
    ?> 
    <?php
    $img = recebe_imagem("http://www.nfe.fazenda.gov.br/scripts/srf/intercepta/captcha.aspx?opt=image ", "receita.gif", "", "receita.txt");
    ?> 
    <img src="receita.gif" /> 
    <form method="POST" action="consulta.php"> 
        captcha 
        <input name='letras' maxlength='4' size='8' /> 
        <br /> 
        codigo acesso 
        <input name='cnpj' maxlength='44' size='60' /> 
        <input type="submit" /> 
    </form> 
</body> 

query.php

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Documento sem título</title> 
</head> 
<body> 
    <?php
    $cnpj = $_POST['cnpj'];
    $letras = $_POST['letras'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "ContentPlaceHolder1$txtChaveAcessoCompleta=$cnpj&ContentPlaceHolder1$txtCaptcha=$letras&ContentPlaceHolder1$btnConsultar=Continuar");
    curl_setopt($ch, CURLOPT_REFERER, "http://www.nfe.fazenda.gov.br/portal/consulta.aspx?tipoConsulta=completa&tipoConteudo=XbSeqxE8pl8= ");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_URL, "http://www.nfe.fazenda.gov.br/portal/consultaCompleta.aspx?tipoConteudo=XbSeqxE8pl8= ");
    $output = curl_exec($ch);
    echo utf8_encode($output);
    ?> 
</body> 

Is there any script to do this better?

    
asked by anonymous 08.05.2014 / 13:57

2 answers

3

If the session is expiring, then there are probably more parameters to be passed to:

http://www.nfe.fazenda.gov.br/portal/consultaCompleta.aspx?tipoConteudo=XbSeqxE8pl8=

Use some debugger to check which parameters the request requests. Based on them, send them.

I can not test here because I do not have a valid NF-e number.

    
08.05.2014 / 14:07
1

I do not work with PHP and what I know about it is too little and insufficient to help.
Much less so I know about cURL and if what I have to pass on to you is really necessary.

However, I already made access to the Portal with Delphi and had to include several fields in the post to then access the page, since the Portal is made in ASP.NET WebForms and this includes several controls.

You have a question in SOen that talks about this access and has these fields: # . It is with Delphi .

This is a list of fields that I believe need to be included in post :

__VIEWSTATE
__EVENTVALIDATION
__EVENTTARGET
__EVENTARGUMENT
ctl00$txtPalavraChave
ctl00$ContentPlaceHolder1$txtChaveAcessoCompleta
ctl00$ContentPlaceHolder1$txtCaptcha
ctl00$ContentPlaceHolder1$btnConsultar=Continuar
hiddenInputToUpdateATBuffer_CommonToolkitScripts=1

The values of __VIEWSTATE , __EVENTVALIDATION and the rest are in inputs of type hidden . You need to pass these values, of course.

This is a demo of the code by the Google Chrome development tools. The other fields are in the form.

Well, here's my two cents on the subject. I hope I can help you.

    
08.05.2014 / 18:35