Skip to content

Commit

Permalink
Alteração de host
Browse files Browse the repository at this point in the history
  • Loading branch information
jansenfelipe committed May 30, 2017
1 parent 55b0f93 commit 5cd748b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 46 deletions.
5 changes: 3 additions & 2 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use JansenFelipe\CpfGratis\CpfGratis;

if(isset($_POST['captcha']) && isset($_POST['cookie']) && isset($_POST['cpf']) && isset($_POST['data_nascimento'])){
$dados = CpfGratis::consulta($_POST['cpf'], $_POST['data_nascimento'], $_POST['captcha'], $_POST['cookie']);
if(isset($_POST['captcha']) && isset($_POST['cookie']) && isset($_POST['token']) && isset($_POST['cpf']) && isset($_POST['data_nascimento'])){
$dados = CpfGratis::consulta($_POST['cpf'], $_POST['data_nascimento'], $_POST['captcha'], $_POST['cookie'], $_POST['token']);
var_dump($dados);
die;
}else
Expand All @@ -16,6 +16,7 @@

<form method="POST">
<input type="hidden" name="cookie" value="<?php echo $params['cookie'] ?>" />
<input type="hidden" name="token" value="<?php echo $params['captchaToken'] ?>" />

<input type="text" name="captcha" placeholder="Captcha" />
<input type="text" name="cpf" placeholder="CPF" />
Expand Down
103 changes: 59 additions & 44 deletions src/CpfGratis.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Goutte\Client;
use JansenFelipe\Utils\Utils;
use Symfony\Component\DomCrawler\Crawler;

class CpfGratis {

Expand All @@ -19,24 +20,31 @@ class CpfGratis {
public static function getParams()
{
$client = new Client();

$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYPEER, false);
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYHOST, false);
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSLVERSION, 4);

$client->request('GET', 'https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaPublica.asp');

$client->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
$client->setHeader('Accept-Encoding', 'gzip, deflate, sdch');
$client->setHeader('Accept-Language', 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2,it;q=0.2');
$client->setHeader('Cache-Control', 'max-age=0');
$client->setHeader('Connection', 'keep-alive');
$client->setHeader('Host', 'cpf.receita.fazenda.gov.br');
$client->setHeader('Upgrade-Insecure-Requests', '1');
$client->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36');

$crawler = $client->request('GET', 'http:https://cpf.receita.fazenda.gov.br/situacao');

$payload = $crawler->filter('#idCaptchaInput')->attr('data-clienteid');

$internal_cookies = $client->getCookieJar()->all()[0];
$cookie = $internal_cookies->getName().'='.$internal_cookies->getValue(). '; path='. $internal_cookies->getPath();

$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_BINARYTRANSFER, true);
$client->request('GET', 'https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/captcha/gerarCaptcha.asp');
$client->request('POST', 'http:https://captcha2.servicoscorporativos.serpro.gov.br/captcha/1.0.0/imagem', [], [], [], $payload);

$image = base64_encode($client->getResponse()->getContent());
$response = explode('@', $client->getResponse()->getContent());

return array(
'cookie' => $cookie,
'captchaBase64' => 'data:image/png;base64,' . $image
'captchaToken' => $response[0],
'captchaBase64' => 'data:image/png;base64,' . $response[1]
);
}

Expand All @@ -47,60 +55,67 @@ public static function getParams()
* @param string $nascimento NASCIMENTO (DDMMYYYY)
* @param string $captcha CAPTCHA
* @param string $stringCookie COOKIE
* @param string $token CAPTCHA TOKEN
* @throws Exception
* @return array Dados da pessoa
*/
public static function consulta($cpf, $nascimento, $captcha, $stringCookie)
public static function consulta($cpf, $nascimento, $captcha, $stringCookie, $token)
{
$arrayCookie = explode(';', $stringCookie);

if (!Utils::isCpf($cpf))
throw new Exception("CPF inválido");

$client = new Client();

$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYPEER, false);
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSL_VERIFYHOST, false);
$client->getClient()->setDefaultOption('config/curl/'.CURLOPT_SSLVERSION, 4);
$client = new Client(['allow_redirects' => false]);

$client->setHeader('Host', 'www.receita.fazenda.gov.br');
$client->setHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0');
$client->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
$client->setHeader('Accept-Language', 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3');
$client->setHeader('Accept-Encoding', 'gzip, deflate');
$client->setHeader('Referer', 'https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaPublica.asp');
$client->setHeader('Cookie', $arrayCookie[0]);
$client->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
$client->setHeader('Accept-Encoding', 'gzip, deflate, sdch');
$client->setHeader('Accept-Language', 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2,it;q=0.2');
$client->setHeader('Cache-Control', 'max-age=0');
$client->setHeader('Connection', 'keep-alive');
$client->setHeader('Cookie', $arrayCookie[0]);
$client->setHeader('Host', 'cpf.receita.fazenda.gov.br');
$client->setHeader('Origin', 'http:https://cpf.receita.fazenda.gov.br');
$client->setHeader('Referer', 'http:https://cpf.receita.fazenda.gov.br/situacao/');
$client->setHeader('Upgrade-Insecure-Requests', '1');
$client->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36');

$param = array(
'txtToken_captcha_serpro_gov_br' => $token,
'txtTexto_captcha_serpro_gov_br' => $captcha,
'tempTxtCPF' => $cpf,
'tempTxtNascimento' => $nascimento,
'temptxtToken_captcha_serpro_gov_br' => $captcha,
'temptxtTexto_captcha_serpro_gov_br' => $captcha
'txtCPF' => $cpf,
'txtDataNascimento' => $nascimento
);

$crawler = $client->request('POST', 'https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaSituacao/ConsultaPublicaExibir.asp', $param);
$crawler = $client->request('POST', 'http:https://cpf.receita.fazenda.gov.br/situacao/ConsultaSituacao.asp', $param);

/*
* Verificando erros
*/
$idMessageError = $crawler->filter('#idMessageError');

if($idMessageError->count() > 0)
throw new Exception(trim($idMessageError->html()));

$clConteudoCompBold = $crawler->filter('span.clConteudoCompBold');

$error = $crawler->filter('span.mensagemErro');
if($clConteudoCompBold->count() > 0)
throw new Exception(trim($clConteudoCompBold->html()));

if($error->count() > 0)
throw new Exception($error->html());
/*
* Buscando dados
*/
$nome = $crawler->filter('#idCnt05 span.clBold')->html();
$nascimento = $crawler->filter('#idCnt13 span.clBold')->html();
$situacao_cadastral = $crawler->filter('#idCnt06 span.clBold')->html();
$situacao_cadastral_data = $crawler->filter('#idCnt14 span.clBold')->html();
$digito_verificador = $crawler->filter('#idCnt07 span.clBold')->html();

$clConteudoDados = $crawler->filter('span.clConteudoDados');
$clConteudoComp = $crawler->filter('span.clConteudoComp');
$hora_emissao = $crawler->filter('#idCnt08 span.clBold')->eq(0)->html();
$data_emissao = $crawler->filter('#idCnt08 span.clBold')->eq(1)->html();
$codigo_controle = $crawler->filter('#idCnt09 span.clBold')->html();

return(array(
'cpf' => Utils::unmask($cpf),
'nome' => trim(str_replace('Nome da Pessoa Física: ', '', $clConteudoDados->eq(1)->filter('b')->html())),
'nascimento' => trim(str_replace('Data de Nascimento: ', '', $clConteudoDados->eq(2)->filter('b')->html())),
'situacao_cadastral' => str_replace('Situação Cadastral: ', '', $clConteudoDados->eq(3)->filter('b')->html()),
'situacao_cadastral_data' => str_replace('Data da Inscrição: ', '', $clConteudoDados->eq(4)->filter('b')->html()),
'digito_verificador' => str_replace('Digito Verificador: ', '', $clConteudoDados->eq(5)->filter('b')->html()),
'hora_emissao' => str_replace('Hora de emissão: ', '', $clConteudoComp->eq(0)->filter('b')->first()->html()),
'data_emissao' => str_replace('Data de emissão: ', '', $clConteudoComp->eq(0)->filter('b')->last()->html()),
'codigo_controle' => str_replace('Código de controle: ', '', $clConteudoComp->eq(1)->filter('b')->html())
));
return compact('nome', 'nascimento', 'situacao_cadastral', 'situacao_cadastral_data', 'digito_verificador', 'hora_emissao', 'data_emissao', 'codigo_controle');
}

}

0 comments on commit 5cd748b

Please sign in to comment.