curl = curl_init(); curl_setopt_array($this->curl, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_USERAGENT => USER_AGENT, CURLOPT_COOKIEJAR => $jar, CURLOPT_COOKIEFILE => $jar, ]); $this->get('landing.do?landing=landing'); $this->home = $this->post("landing.do", ["accept" => "Accept"]); } function get(string $path): string { curl_setopt($this->curl, CURLOPT_HTTPGET, true); curl_setopt($this->curl, CURLOPT_URL, BASE . $path); return $this->run(); } function post(string $path, array $fields): string { curl_setopt($this->curl, CURLOPT_POST, true); curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($fields)); curl_setopt($this->curl, CURLOPT_URL, BASE . $path); curl_setopt($this->curl, CURLOPT_REFERER, BASE . $path); return $this->run(); } /** Sends the prepared request, paced so a lookup doesn't hammer a shared public site. */ private function run(): string { usleep(PAUSE_MICROSECONDS); $body = curl_exec($this->curl); if ($body === false) { fail('Court site request failed: ' . curl_error($this->curl)); } return $body; } } function load_dom(string $html): DOMXPath { $doc = new DOMDocument(); @$doc->loadHTML('' . $html); return new DOMXPath($doc); } function cell_text(DOMNode $node): string { return clean_field($node->textContent); } /** Returns name => fips for every court the site offers. */ function list_courts(Session $session): array { $html = $session->home; preg_match_all('/name="courtName" value="([^"]+)"/', $html, $names); preg_match_all('/name="courtFips" value="([^"]+)"/', $html, $fips); if (!$names[1] || count($names[1]) !== count($fips[1])) { fail('Could not read the court list'); } return array_combine($names[1], $fips[1]); } /** Finds one court by FIPS code or by a name fragment ("arlington", "fairfax-county"). Returns [name, fips]. */ function resolve_court(array $courts, string $wanted): array { $needle = strtolower(str_replace('-', ' ', trim($wanted))); foreach ($courts as $name => $fips) { if ($fips === $needle) { return [$name, $fips]; } } $matches = array_filter($courts, fn($name) => str_contains(strtolower($name), $needle), ARRAY_FILTER_USE_KEY); foreach ($matches as $name => $fips) { if (strtolower($name) === "$needle general district court") { return [$name, $fips]; } } if (count($matches) === 1) { return [array_key_first($matches), reset($matches)]; } fail($matches ? "Ambiguous court '$wanted': " . implode(', ', array_keys($matches)) : "No court matches '$wanted'"); } function select_court(Session $session, string $name, string $fips): void { $session->post('changeCourt.do', ['selectedCourtsName' => $name, 'selectedCourtsFipCode' => $fips]); } /** Case rows from a name search: [case, name, hearing_date, hearing_time, charge, note]. */ function parse_results(string $html): array { $xp = load_dom($html); $rows = []; foreach ($xp->query('//tr[.//a[contains(@onclick, "caseDetailsPayment(\'caseDetails\'")]]') as $tr) { $cells = []; foreach ($xp->query('./td', $tr) as $td) { $cells[] = cell_text($td); } $cells = array_values(array_filter($cells, fn($c) => $c !== '')); if (count($cells) >= 5) { $rows[] = array_slice(array_pad($cells, 6, ''), 0, 6); } } return $rows; } function search_name(Session $session, string $fips, string $division, string $last, string $first, string $category): array { $session->get("nameSearch.do?fromSidebar=true&formAction=searchLanding&searchDivision=$division&searchFipsCode=$fips&localFipsCode=$fips"); $html = $session->post('nameSearch.do', [ 'formAction' => 'newSearch', 'localFipsCode' => $fips, 'searchFipsCode' => $fips, 'searchDivision' => $division, 'searchType' => 'name', 'lastName' => $last, 'firstName' => $first, 'searchCategory' => $category, 'localnamesearchlastName' => $last, 'localnamesearchfirstName' => $first, 'localnamesearchsearchCategory' => $category, ]); return parse_results($html); } /** Case detail as [section, label, value] rows, one per labelled field. */ function parse_case(string $html): array { $xp = load_dom($html); $rows = []; foreach ($xp->query("//td[contains(@class,\"subheader\")][@aria-controls]") as $header) { $section = trim(preg_replace("/\s+Information$/", "", cell_text($header))); $body = $xp->query("//*[@id=\"" . $header->getAttribute("aria-controls") . "\"]")->item(0); if (!$body) { continue; } foreach ($xp->query(".//td[starts-with(@class,\"labelgrid\")]", $body) as $label) { $value = $xp->query("following-sibling::td[1][starts-with(@class,\"labelvalue\")]", $label)->item(0); $name = rtrim(cell_text($label), " :"); if ($name !== "") { $rows[] = [$section, $name, $value ? cell_text($value) : ""]; } } } return $rows; } function hearings(string $html): array { $xp = load_dom($html); $rows = []; foreach ($xp->query('//tr[td]') as $tr) { $cells = []; foreach ($xp->query('./td', $tr) as $td) { $cells[] = cell_text($td); } if (isset($cells[0]) && preg_match('#^\d\d/\d\d/\d{4}$#', $cells[0]) && isset($cells[1]) && preg_match('/^\d+:\d\d [AP]M$/', $cells[1])) { $rows[] = array_slice(array_pad($cells, 7, ''), 0, 7); } } return $rows; } function case_detail(Session $session, string $fips, string $division, string $last, string $first, string $caseNumber): string { search_name($session, $fips, $division, $last, $first, 'O'); return $session->post('nameSearch.do', [ 'formAction' => 'caseDetails', 'displayCaseNumber' => $caseNumber, 'localFipsCode' => $fips, 'caseActive' => 'true', 'localLastName' => $last, 'searchFipsCode' => $fips, 'searchDivision' => $division, 'searchType' => 'name', 'lastName' => $last, 'firstName' => $first, 'searchCategory' => 'O', 'clientSearchCounter' => '1', ]); }