IEC (INSS Specific Registry) Query for Data Return

Asked

Viewed 1,547 times

1

I am trying to develop a query class that will return the data available by the IEC (INSS).

So far I have the following codes:

/**
         * Retorna os parâmetros necessários para a consulta;
         * @throws Exception
*/
public static function getParams()
{
    require 'vendor/autoload.php';

    $client = new GuzzleHttp\Client();
    $crawler = $client->get('http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml');

    $body = $crawler->getBody();
    $headers = $crawler->getHeaders();
    $cookie = $headers['Set-Cookie'][1];

    if (!method_exists('phpQuery', 'newDocumentHTML'))
        require_once __DIR__ . DIRECTORY_SEPARATOR . 'phpQuery-onefile.php';

    $doc = phpQuery::newDocumentHTML($body, $charset = 'utf-8');
    $token = phpQuery::pq('form#formInicial input[name="DTPINFRA_TOKEN"]')->val();
    $viewstate = phpQuery::pq('form#formInicial input[name="javax.faces.ViewState"]')->val();
    $imgCaptcha = phpQuery::pq('form#formInicial img[name="formInicial:j_id41"]')->attr('src');

    $urlCaptcha = 'http://www2.dataprev.gov.br' . $imgCaptcha;
    $captchaBase64 = 'data:image/png;base64,' . base64_encode(file_get_contents($urlCaptcha));

    if ($viewstate == '')
        throw new Exception('Erro ao recuperar viewstate');

    return [
            'captcha' => $urlCaptcha,
            'captchaBase64' => $captchaBase64,
            'viewstate' => $viewstate,
            'cookie' => $cookie,
            'token' => $token,
            ];
}

/**
     * Metodo para realizar a consulta
     *
     * @param  string $cei CEI
     * @param  string $captcha Captcha
     * @param  string $viewstate ViewState
     * @param  string $token Token
     * @throws Exception
     * @return array  Dados da empresa
*/
public static function consulta($cei, $captcha, $viewstate, $token, $stringCookie)
{
    require 'vendor/autoload.php';
    $arrayCookie = explode(';', $stringCookie);
    $urlCurl = 'http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml';

     $client = new GuzzleHttp\Client();
     $param = [
               'body' => [
               'formInicial' => 'formInicial',
               'DTPINFRA_TOKEN' => $token,
               'formInicial:categoria' => 'EMPRESA',
               'formInicial:tipoDoDocumento' => 'CEI',
               'formInicial:cei' => $cei,
               'formInicial:captchaId' => $captcha,
               'formInicial:botaoPrincipalConfirmar' => 'Confirmar',
               'javax.faces.ViewState' => $viewstate
               ]
               ];
      $request = $client->createRequest('POST', $urlCurl, $param);
      $request->setHeader('Host', 'www2.dataprev.gov.br');
      $request->setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0');
      $request->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
      $request->setHeader('Accept-Language', 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4');
      $request->setHeader('Accept-Encoding', 'gzip, deflate');
      $request->setHeader('Referer', 'http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml');
      $request->setHeader('Cookie', $arrayCookie[0]);
      $request->setHeader('Connection', 'keep-alive');
      $response = $client->send($request);
      echo $response;
}  

But I have the following problem:

The site works with session and sends the request form to the same page validating that session;

When sending the method consulta(), usually there are errors like expired session or it just loads the start screen by filling in the data.

Someone has done something similar or could help me solve this situation?

Website for consultation:

http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml

  • Could you put your solution as a comment, so that others can enjoy this solution?

  • It would be better to put as an answer.

  • As requested, the solution was made available to users.

1 answer

2


As requested, it follows resolution of the problem. Remembering the use of external classes:

  • Guzzlehttp: http://guzzle.readthedocs.org/en/latest/
  • Phpquery: https://code.google.com/p/phpquery/

    /**
     * Classe para consulta de dados utilizando CEI (Cadastro Específico do INSS)
     * @version 1.0
     * Class ConsultaCei
     */
    class ConsultaCei
    {
    
        /**
         * Retorna os parâmetros necessários para a consulta;
         * @return array
         * @throws \Exception
         */
        public static function getParams()
        {
            require 'vendor/autoload.php';
            $client = new GuzzleHttp\Client();
            $response = $client->get('http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml');
    
            $body = $response->getBody();
            $headers = $response->getHeaders();
            $cookie = '';
            foreach ($headers['Set-Cookie'] as $cook) {
                $cookie .= trim($cook) . ';';
            }
    
            if (!method_exists('phpQuery', 'newDocumentHTML'))
                require_once __DIR__ . DIRECTORY_SEPARATOR . 'phpQuery-onefile.php';
    
            $doc = phpQuery::newDocumentHTML($body, $charset = 'utf-8');
            $token = phpQuery::pq('form#formInicial input[name="DTPINFRA_TOKEN"]')->val();
            $viewstate = phpQuery::pq('form#formInicial input[name="javax.faces.ViewState"]')->val();
            $imgCaptcha = phpQuery::pq('form#formInicial img[name="formInicial:j_id41"]')->attr('src');
            $urlCaptcha = 'http://www2.dataprev.gov.br' . $imgCaptcha;
            $captchaBase64 = 'data:image/png;base64,' . base64_encode(file_get_contents($urlCaptcha));
    
            if ($viewstate == '')
                throw new Exception('Erro ao recuperar viewstate');
    
            return [
                'captcha' => $urlCaptcha,
                'captchaBase64' => $captchaBase64,
                'viewstate' => $viewstate,
                'cookie' => $cookie,
                'token' => $token,
            ];
        }
    
        /**
         * Realizer a consulta
         * @param  string $cei CEI
         * @param  string $captcha Captcha
         * @param  string $token Token
         * @param  string $strCookie
         * @param  string $viewstate ViewState
         * @throws Exception
         * @return array
         */
        public static function consulta($cei, $captcha, $token, $strCookie, $viewstate)
        {
            require 'vendor/autoload.php';
            $arrayCookie = explode(';', $strCookie);
            $session = ';' . trim($arrayCookie[3]);
            $urlCurl = 'http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml' . $session;
            $client = new GuzzleHttp\Client();
    
            $request = $client->createRequest('POST', $urlCurl, [
                'body' => [
                    'formInicial' => 'formInicial',
                    'DTPINFRA_TOKEN' => $token,
                    'formInicial:categoria' => 'EMPRESA',
                    'formInicial:tipoDoDocumento' => 'CEI',
                    'formInicial:cei' => $cei,
                    'formInicial:captchaId' => $captcha,
                    'formInicial:botaoPrincipalConfirmar' => 'Confirmar',
                    'javax.faces.ViewState' => $viewstate
                ],
                'headers' => [
                    'Host' => 'www2.dataprev.gov.br',
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Language' => 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4',
                    'Accept-Encoding' => 'gzip, deflate',
                    'Referer' => 'http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml',
                    'Cookie' => $strCookie,
                    'Connection' => 'keep-alive'
                ],
            ]);
            $client->send($request);
    
            $a = trim(preg_replace('/[^0-9\s]/', '', explode(':', $viewstate)[1]));
            $viewstate = str_replace($a, $a + 1, $viewstate);
    
            $request2 = $client->createRequest('POST', $urlCurl, [
                'body' => [
                    'formInicial' => 'formInicial',
                    'DTPINFRA_TOKEN' => $token,
                    'formInicial:categoria' => 'EMPRESA',
                    'formInicial:tipoDoDocumento' => 'CEI',
                    'formInicial:cei' => $cei,
                    'formInicial:captchaId' => $captcha,
                    'formInicial:botaoPrincipalConfirmar' => 'Confirmar',
                    'javax.faces.ViewState' => $viewstate
                ],
                'headers' => [
                    'Host' => 'www2.dataprev.gov.br',
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Language' => 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4',
                    'Accept-Encoding' => 'gzip, deflate',
                    'Referer' => 'http://www2.dataprev.gov.br/PortalSalInternet/faces/pages/calcContribuicoesEmpresasEOrgaosPublicos/inicio.xhtml',
                    'Cookie' => $strCookie,
                    'Connection' => 'keep-alive'
                ],
            ]);
            $response2 = $client->send($request2);
            $body = $response2->getBody();
    
            if (!method_exists('phpQuery', 'newDocumentHTML'))
                require_once __DIR__ . DIRECTORY_SEPARATOR . 'phpQuery-onefile.php';
    
            $doc = phpQuery::newDocumentHTML($body, $charset = 'utf-8');
            $result = [];
            foreach (phpQuery::pq('div.linhaTipoB') as $dado) {
                $chave = trim(preg_replace('/\s+/', ' ', phpQuery::pq($dado)->find('span:first')->html()));
                $chave = preg_replace('/[^A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ\s]/', '', $chave);
                switch ($chave) {
                    case 'CEI':
                        $chave = 'cei';
                        break;
                    case 'Razão Social':
                        $chave = 'razao_social';
                        break;
                    case 'Endereço':
                        $chave = 'endereco';
                        break;
                    case 'Bairro':
                        $chave = 'bairro';
                        break;
                    case 'Município':
                        $chave = 'municipio';
                        break;
                    case 'UF':
                        $chave = 'uf';
                        break;
                    case 'CEP':
                        $chave = 'cep';
                        break;
                }
                $valor = htmlspecialchars_decode(trim(preg_replace('/\s+/', ' ', phpQuery::pq($dado)->find('span.flutua')->html())));
                $result[$chave] = $valor;
            }
            return $result;
        }
    }
    

Example of use:

require_once __DIR__ . DIRECTORY_SEPARATOR . 'ConsultaCei.php';
$cei = new ConsultaCei();
$params = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $resultado = $cei->consulta($_POST['cei'], $_POST['captcha'], $_POST['token'], $_POST['cookie'], $_POST['viewstate']);
    var_dump($resultado);
} else {
    $params = $cei->getParams();
    var_dump($params);

<form method="POST" name="formInicial" id="formInicial">
    <input type="hidden" name="viewstate" value="<?= $params['viewstate'] ?>">
    <input type="hidden" name="cookie" value="<?= $params['cookie'] ?>">
    <input type="hidden" name="token" value="<?= $params['token'] ?>">
    <input type="hidden" name="cei" value="15.902.00984/03">
    <img src="<?= $params['captchaBase64'] ?>"><br>
    Digite o código da imagem: <input type="input" name="captcha" value=""><br>
    <input type="submit" value="Submit">
</form>

Browser other questions tagged

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