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:
Could you put your solution as a comment, so that others can enjoy this solution?
– Felipe Avelar
It would be better to put as an answer.
– mutlei
As requested, the solution was made available to users.
– Rafael Withoeft