Skip to main content
4 de 4
removeu 2 caracteres do conteúdo
Rafael Withoeft
  • 2,3mil
  • 15
  • 31

Conforme solicitação, segue resolução do problema. Lembrando da utilização de classes externas:

  • GuzzleHttp: http://guzzle.readthedocs.org/en/latest/

  • PhpQuery: https://code.google.com/p/phpquery/

    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; } }

Exemplo de uso:

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>
Rafael Withoeft
  • 2,3mil
  • 15
  • 31