Log into the Nfe portal with Curl and PHP

Asked

Viewed 2,780 times

5

I’ve done a lot of research on this and so far I haven’t been able to do anything concrete. Is this possible?

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

With this occurs 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> 

php query.

<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 run this better?

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’t test here because I don’t have a valid NF-e number.

  • 2

    Valew, the system is running here for testing> http://www.meifacil.com.br/system/estudo/nfe/ ’s test access code> 33110733068883000201550010115687931122936510 .

1

I don’t work with PHP and what I know about him is very little and insufficient to help.
Let alone then know about cURL and if what I have to pass on to you is really necessary.

But I already made an access to the Portal with Delphi and I had to include several fields in the post to then be able to access the page, because the Portal is made in ASP.NET WebForms and this includes several controls.

Have a question on Soen who talks about this access and has these fields: idhttp-idcookiemanager-Asp-net-webform-page-close. Is with the Delphi.

This is a list of the fields I believe need to include 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 the kind Hidden. You need to pass these values, of course.

input´s hidden

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

Well, there’s my two cents on the subject.
I hope I can help you.

  • Thank you more as I get the values of these fields: _VIEWSTATE __EVENTVALIDATION __EVENTTARGET _EVENTARGUMENT ctl00$txtPalavraChave Can you tell me?

  • Thanks more help I will need to pay for the service because I am not able to do it. If you have someone to indicate thank you.

Browser other questions tagged

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