<?php



function trazer1($str, $start, $end) {
    $parts = explode($start, $str);
    if (isset($parts[1])) {
        $subParts = explode($end, $parts[1]);
        return $subParts[0];
    }
}




// User-Agents reais e modernos (2024–2025)
$userAgents = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.6367.208 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.6422.78 Safari/537.36"
];

$userAgent = $userAgents[array_rand($userAgents)];



function extrairCA($html) {
    $cas = [];

    $padrao = "/\/consultapublica\/ConsultaPublica\/DetalheProcessoConsultaPublica\/listView\.seam\?ca=([a-f0-9]{40,})/";


    // Procura todas as ocorrências
    if (preg_match_all($padrao, $html, $matches)) {
        $cas = array_unique($matches[1]); // Remove duplicatas
    }

    return $cas;
}





$url = "https://pje1g.trf1.jus.br/consultapublica/ConsultaPublica/listView.seam";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
    'Host: pje1g.trf1.jus.br',
    'Connection: keep-alive',
    'Cache-Control: max-age=0',
    'sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
    'sec-ch-ua-mobile: ?0',
    'sec-ch-ua-platform: "Windows"',
    'Upgrade-Insecure-Requests: 1',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7'
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, __DIR__ . "/cookies.txt"); // Salva cookies
$resp = curl_exec($curl);



$tk = trazer1($resp, 'input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="','"');


// $ch = curl_init();

// Defina a URL e as opções de cURL
curl_setopt($curl, CURLOPT_URL, 'https://pje1g.trf1.jus.br/consultapublica/ConsultaPublica/listView.seam');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Retorna a resposta em vez de exibir
curl_setopt($curl, CURLOPT_POST, true); // Define o método POST
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); // Define o método POST
curl_setopt($curl, CURLOPT_HEADER, true); // Define o método POST
curl_setopt($curl, CURLOPT_ENCODING, true); // Define o método POST
curl_setopt($curl, CURLOPT_POSTFIELDS, "AJAXREQUEST=_viewRoot&fPP%3AnumProcesso-inputNumeroProcessoDecoration%3AnumProcesso-inputNumeroProcesso=&mascaraProcessoReferenciaRadio=on&fPP%3Aj_id152%3AprocessoReferenciaInput=&fPP%3Adnp%3AnomeParte=&fPP%3Aj_id170%3AnomeAdv=&fPP%3Aj_id179%3AclasseProcessualProcessoHidden=&tipoMascaraDocumento=on&fPP%3AdpDec%3AdocumentoParte=&fPP%3ADecoration%3AnumeroOAB=7777&fPP%3ADecoration%3Aj_id214=&fPP%3ADecoration%3AestadoComboOAB=6&fPP=fPP&autoScroll=&javax.faces.ViewState=$tk&fPP%3Aj_id220=fPP%3Aj_id220&AJAX%3AEVENTS_COUNT=1&"); // Envia os dados do formulário
curl_setopt($curl, CURLOPT_HTTPHEADER, [
   'Host: pje1g.trf1.jus.br',
    'Connection: keep-alive',
    // 'Content-Length: 526',
    'sec-ch-ua-platform: "Windows"',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
    'sec-ch-ua: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'sec-ch-ua-mobile: ?0',
    'Accept: */*',
    'Origin: https://pje1g.trf1.jus.br',
    'Sec-Fetch-Site: same-origin',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Dest: empty',
    'Referer: https://pje1g.trf1.jus.br/consultapublica/ConsultaPublica/listView.seam',
]);

// Configura para armazenar cookies e manter a sessão
curl_setopt($curl, CURLOPT_COOKIEJAR, __DIR__ . "/cookies.txt"); // Salva cookies
curl_setopt($curl, CURLOPT_COOKIEFILE, __DIR__ . "/cookies.txt"); // Usa cookies salvos
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); // Usa cookies salvos
$html = curl_exec($curl);







// // Exemplo de uso:
// $listaCA = extrairCA($html);
// // echo count($listaCA);

// // exit;
// foreach ($listaCA as $ca) {
//     echo "CA encontrado: $ca\n";
// }
// exit;


