Guest User

Untitled

a guest
Jan 20th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.57 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace SimpleApi\Litispendencia;
  5.  
  6. include(app_path() . '/HtmlDomParser/simple_html_dom.php');
  7.  
  8. class ConsultaProcessualPublicaServico
  9. {
  10.     protected $urlBase = 'https://portal.trf2.jus.br/portal/consulta/cons_procs.asp';
  11.  
  12.     const DIRETORIO_COOKIE = '/cookies';
  13.  
  14.     public function __construct()
  15.     {
  16.  
  17.     }
  18.  
  19.     public function enviarDadosConsulta()
  20.     {
  21.         $caminhoCookieProcesso = $this->getCaminhoCookie();
  22.  
  23.         $html = $this->getHtml($this->urlBase);
  24.  
  25.         //$divFormulario = $html->find('#ConsProc');
  26.         //$divTable = $divFormulario[0]->children(12);
  27.         //$divDados = $divTable->find('tr');
  28.        
  29.         $numeroProcesso = '0001541-89.2006.4.02.5101';
  30.  
  31.         $campos = [
  32.             'Validar' => '',
  33.             'CampoFoco' => '',
  34.             'Botao' => 'Pesquisar',
  35.             'EstatCont' => '117829495', //$divFormulario[0]->children(3)->value,
  36.             'C' => '',
  37.             'A' => '',
  38.             'UsarCaptcha' => 'S', //$divFormulario[0]->children(6)->value,
  39.             'FecharSessao' => '',
  40.             'captcha' => '',
  41.             'gabarito' => '1', //$divFormulario[0]->children(9)->value,
  42.             'resposta'  => '1', //$divFormulario[0]->children(10)->value,
  43.             'Localidade' => '0',
  44.             'baixado' => '0',
  45.             'CodLoc' => '',
  46.             'NumProc' => $numeroProcesso,
  47.             'CodDoc' => '',
  48.             'NumProcOrig' => '',
  49.             'CodOAB' => '',
  50.             'CodAdv' => '',
  51.             'TipoDocPess' => '0',
  52.             'NumDocPess' => '',
  53.             'CodTipoDocPess' => '',
  54.             'NomeParte' => '',
  55.             'NomeAdv' => '',
  56.             'NumInq' => '',
  57.             'captchaCode' => '1'
  58.         ];
  59.  
  60.         $htmlPrimeiraRequisicao = $this->getHtmlComCurlPost($this->urlBase, $caminhoCookieProcesso, $campos);
  61.      
  62.         $htmlSegundaRequisicao = $this->getHtmlComCurl($this->urlBase, $caminhoCookieProcesso);
  63.         dd($htmlSegundaRequisicao->plaintext);
  64.         //$informacoes = $htmlTerceiraRequisicao->find('#ResListProc');
  65.      
  66.         //return $informacoes;
  67.     }
  68.  
  69.     protected function prepararCamposPost(array $campos)
  70.     {  
  71.         return http_build_query([
  72.             'Validar' => $campos['Validar'],
  73.             'CampoFoco' => $campos['CampoFoco'],
  74.             'Botao' => $campos['Botao'],
  75.             'EstatCont' => $campos['EstatCont'],
  76.             'C' => $campos['C'],
  77.             'A' => $campos['A'],
  78.             'UsarCaptcha' => $campos['UsarCaptcha'],
  79.             'FecharSessao' => $campos['FecharSessao'],
  80.             'captcha'  => $campos['captcha'],
  81.             'gabarito'  => $campos['gabarito'],
  82.             'resposta'  => $campos['resposta'],
  83.             'Localidade' =>  $campos['Localidade'],
  84.             'CodLoc' => $campos['CodLoc'],
  85.             'NumProc' =>   $campos['NumProc'],
  86.             'CodDoc' => $campos['CodDoc'],
  87.             'NumProcOrig' => $campos['NumProcOrig'],
  88.             'CodOAB' =>  $campos['CodOAB'],
  89.             'CodAdv' =>  $campos['CodAdv'],
  90.             'TipoDocPess' => $campos['TipoDocPess'],
  91.             'NumDocPess' => $campos['NumDocPess'],
  92.             'CodTipoDocPess' => $campos['CodTipoDocPess'],
  93.             'NomeParte' => $campos['NomeParte'],
  94.             'NomeAdv' => $campos['NomeAdv'],
  95.             'NumInq' => $campos['NumInq'],
  96.             'captchaCode' => $campos['captchaCode']
  97.         ]);
  98.     }
  99.  
  100.  
  101.     protected function getHtml(string $url): \simple_html_dom
  102.     {
  103.         return file_get_html($url, false, null, 0);
  104.     }
  105.  
  106.       /**
  107.      * @return string
  108.      * @throws \Exception
  109.      */
  110.     protected function getCaminhoCookie(): string
  111.     {
  112.         $cookiesDiretorio = storage_path(self::DIRETORIO_COOKIE);
  113.  
  114.         if (!is_dir($cookiesDiretorio)) {
  115.             mkdir($cookiesDiretorio);
  116.         }
  117.  
  118.         $hoje = (new \DateTime('now'))->format('YmdHisu');
  119.  
  120.         return "{$cookiesDiretorio}/cookie-{$hoje}.txt";
  121.     }
  122.  
  123.  
  124.     protected function getHtmlComCurl(string $url, string $caminhoCookie): \simple_html_dom
  125.     {
  126.         // Inicializa o objeto CURL
  127.         $curl = curl_init();
  128.  
  129.         // Configurações do CURL
  130.         curl_setopt($curl, CURLOPT_URL, $url);
  131.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  132.         curl_setopt($curl, CURLOPT_COOKIEFILE, $caminhoCookie);
  133.         curl_setopt($curl, CURLOPT_COOKIEJAR, $caminhoCookie);
  134.      
  135.         $resultado = curl_exec($curl);
  136.  
  137.         curl_close($curl);
  138.  
  139.         $html = new \simple_html_dom();
  140.  
  141.         return $html->load($resultado);
  142.     }
  143.    
  144.     protected function getHtmlComCurlPost(string $url, string $caminhoCookie, array $campos): \simple_html_dom
  145.     {
  146.        
  147.         // Inicializa o objeto CURL
  148.         $curl = curl_init();
  149.  
  150.         // Configurações do CURL
  151.         curl_setopt($curl, CURLOPT_URL, $url);
  152.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  153.         curl_setopt($curl, CURLOPT_COOKIEFILE, $caminhoCookie);
  154.         curl_setopt($curl, CURLOPT_COOKIEJAR, $caminhoCookie);
  155.         // Requisição POST
  156.         curl_setopt($curl, CURLOPT_POST, true);
  157.         // Campos do POST
  158.  
  159.         $resultado = curl_exec($curl);
  160.  
  161.         curl_close($curl);
  162.  
  163.         $html = new \simple_html_dom();
  164.  
  165.         return $html->load($resultado);    
  166.     }
  167.  
  168.     protected function removerEspacos(string $string): string
  169.     {
  170.         return trim(str_replace('&nbsp;', '', $string));
  171.     }
  172.  
  173.     protected function tratarTexto(string $string): string
  174.     {
  175.         return $this->removerEspacos(html_entity_decode($string));
  176.     }
  177. }
Add Comment
Please, Sign In to add comment