Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Divida_ativa {
- private function get_botstation_captcha_response($task_id) {
- $curl = curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_URL => "http://local.botstation.com/api/task?auth_token=ABC123&service=recaptcha_v2&task_id=$task_id",
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => "GET",
- ));
- $response = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err) {
- return false;
- } else {
- return json_decode($response);
- }
- }
- private function create_botstation_captcha($site_key, $url) {
- $curl = curl_init();
- curl_setopt_array($curl, array(
- CURLOPT_URL => "http://local.botstation.com/api/task",
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 30,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => "POST",
- // CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"auth_token\"\r\n\r\nABC123\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"service\"\r\n\r\nrecaptcha_v2\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"siteKey\"\r\n\r\n$site_key\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"url\"\r\n\r\n$url\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
- CURLOPT_POSTFIELDS => http_build_query(["auth_token" => "ABC123", "service" => "recaptcha_v2", "siteKey" => $site_key, "url" => $url]),
- ));
- $response = curl_exec($curl);
- $err = curl_error($curl);
- curl_close($curl);
- if ($err) {
- return false;
- } else {
- return json_decode($response);
- }
- }
- public function solve_captcha($site_key, $url, $tries = 0) {
- // tenta no AZ primerio, se nao conseguir vai p o botstation
- if ($task = $this->create_botstation_captcha($site_key, $url)) {
- if ($task->status && $task->result->task_id) {
- $response = $this->get_botstation_captcha_response($task->result->task_id);
- $timer = 0;
- while (in_array($response->result->status, ["QUEUED", "RUNNING"])) {
- sleep(1);
- $timer ++;
- $response = $this->get_botstation_captcha_response($task->result->task_id);
- }
- if (!empty($response->result->output->token)) {
- return $response->result->output->token;
- } else {
- if ($tries <= 3) {
- $tries++;
- return $this->solve_captcha($site_key, $url, $tries);
- } else {
- return false;
- }
- }
- }
- }
- return false;
- }
- private function parse_rows($html) {
- $debitos = [];
- $total = 0;
- $doc = new DOMDocument();
- $doc->loadHTML($html);
- $tables = $doc->getElementsByTagName('table');
- if (!empty($tables)) {
- foreach ($tables as $key => $tmp) {
- if ($tmp->getAttribute("class") != "alternate") {
- continue;
- }
- $table = $tmp;
- }
- }
- $rows = $table->getElementsByTagName("tr");
- foreach ($rows as $row) {
- if (!preg_match("/[0-9\.\,]/", $row->textContent))
- continue;
- $valor = preg_replace("/\,/", ".", preg_replace("/[^0-9\,]/", "", trim($row->textContent)));
- $debitos[] = $valor;
- $total += $valor;
- }
- return ['total' => $total, 'debitos' => $debitos];
- }
- public function consulta($renavam, $tries = 0) {
- try {
- print_r("consulta renavam: {$renavam} started\n");
- $captcha_token = $this->solve_captcha('6LeeDDoUAAAAAL7awoPJgSMuiF6AuJW5rf0zqEfy', 'https://www.dividaativa.pge.sp.gov.br');
- if ($captcha_token) {
- print_r("captcha solved\n");
- $start_time = time();
- $options = array(
- CURLOPT_RETURNTRANSFER => true, // return web page
- CURLOPT_HEADER => false, // don't return headers
- CURLOPT_FOLLOWLOCATION => true, // follow redirects
- CURLOPT_USERAGENT => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36", // who am i
- CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
- );
- //
- /** JSESSIONID */
- $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/");
- curl_setopt_array($handle, $options);
- curl_setopt($handle, CURLOPT_HEADER, true);
- $response = curl_exec($handle);
- curl_close($handle);
- $m = [];
- preg_match("/Set\-Cookie\:\sJSESSIONID=(?P<JSESSIONID>.*)\;/", $response, $m);
- $cookie = "Cookie: JSESSIONID=" . $m['JSESSIONID'] . "; path=/da-ic-web;";
- // print_r($cookie);
- $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/da-ic-web/consultaDebitosComunicadoCadinVerificaCaptcha.do");
- curl_setopt_array($handle, $options);
- curl_setopt($handle, CURLOPT_HTTPHEADER, [$cookie, 'Content-Type: application/x-www-form-urlencoded']);
- curl_setopt($handle, CURLOPT_POST, 1);
- curl_setopt($handle, CURLOPT_POSTFIELDS, "tipoDocumento=7&g-recaptcha-response={$captcha_token}&valorDocumento={$renavam}&tipoConsulta=termoAceite&operation=buscar");
- $response = curl_exec($handle);
- curl_close($handle);
- print_r("html OK\n");
- $doc = new DOMDocument();
- @$doc->loadHTML($response);
- $debitos = [];
- $debitos_return = [];
- $total = 0;
- if ($doc->getElementById('idTipoDebitoCliente')) {
- $tmp = $this->parse_rows($response);
- $debitos_return = $tmp['debitos'];
- $total = $tmp['total'];
- } else {
- preg_match_all("/preencheHidden\(\'idDebito\'\,\s\'([0-9]*)\'\,\s\'idPessoa\'\,\s\'([0-9]*)\'\)/", $response, $tmp);
- $debitos = $tmp[1];
- $ids = $tmp[2];
- }
- if (!empty($debitos)) {
- foreach ($debitos as $key => $debito) {
- $handle = curl_init("https://www.dividaativa.pge.sp.gov.br/da-ic-web/escolherDevedorDebito.do");
- curl_setopt_array($handle, $options);
- curl_setopt($handle, CURLOPT_HTTPHEADER, [$cookie, 'Content-Type: application/x-www-form-urlencoded']);
- curl_setopt($handle, CURLOPT_POST, 1);
- curl_setopt($handle, CURLOPT_POSTFIELDS, "idDebito={$debito}&idPessoa={$ids[$key]}&operation=selecionar&devedor=1");
- $response = curl_exec($handle);
- curl_close($handle);
- $tmp = $this->parse_rows($response);
- $debitos_return = array_merge($debitos_return, $tmp['debitos']);
- $total += $tmp['total'];
- }
- }
- print_r("executed in " . (time() - $start_time ) . " seconds\n");
- return ["success" => true, "debitos" => $debitos_return, "total" => $total];
- } else {
- return ["success" => false, "msg" => "Captcha Error!"];
- }
- } catch (Exception $ex) {
- if ($tries <= 3) {
- $tries++;
- return $this->consulta($renavam, $tries);
- } else {
- return ["success" => false, "msg" => "Órgão fora de operação!"];
- }
- }
- }
- }
- $da = new Divida_ativa();
- //print_r($da->consulta('123123123'));
- print_r($da->consulta('945509537'));
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement