true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 30, CURLOPT_USERAGENT => USER_AGENT, ]); $body = curl_exec($curl); if ($body === false) { fail('Jail site request failed: ' . curl_error($curl)); } return $body; } /** Absolute detail-page URLs for everyone in custody matching the name. */ function find_inmates(string $base, string $last, string $first): array { $name = $first === '' ? $last : "$last, $first"; $html = fetch($base . '?' . http_build_query(['Name' => $name, 'InCustody' => 'True'])); preg_match_all('#href="(/NewWorld\.InmateInquiry/[^"]+/Inmate/Detail/[^"]+)"#', $html, $links); $origin = parse_url($base, PHP_URL_SCHEME) . '://' . parse_url($base, PHP_URL_HOST); return array_map(fn($path) => $origin . $path, array_unique($links[1])); } function field_list(DOMXPath $xp, DOMNode $context): array { $fields = []; foreach ($xp->query('.//ul[contains(@class,"FieldList")]/li', $context) as $li) { $label = $xp->query('./label', $li)->item(0); $value = $xp->query('./span', $li)->item(0); if ($label) { $fields[cell_text($label)] = $value ? cell_text($value) : ''; } } return $fields; } /** Rows of the grid inside the given div class, as lists of cell text; "No data" rows dropped. */ function grid(DOMXPath $xp, DOMNode $context, string $class): array { $rows = []; foreach ($xp->query(".//div[contains(@class,\"$class\")]//tbody/tr", $context) as $tr) { $cells = []; foreach ($xp->query('./td', $tr) as $td) { $cells[] = cell_text($td); } if (count($cells) > 1) { $rows[] = $cells; } } return $rows; } /** One person's detail page as ['inmate' => fields, 'bookings' => [...]]. */ function parse_inmate(string $html): array { $xp = load_dom($html); $demographics = $xp->query('//div[@id="DemographicInformation"]')->item(0); $bookings = []; foreach ($xp->query('//div[@id="BookingHistory"]//div[h3]') as $booking) { $number = $xp->query('./h3/span', $booking)->item(0); $bookings[] = [ 'fields' => ['Booking' => $number ? cell_text($number) : ''] + field_list($xp, $booking), 'bonds' => grid($xp, $booking, 'BookingBonds'), 'courts' => array_map(fn($r) => array_slice($r, 1, 2), grid($xp, $booking, 'BookingCourtInfo')), 'charges' => array_map(fn($r) => [$r[1], $r[2], $r[3], $r[5], $r[6], $r[7]], grid($xp, $booking, 'BookingCharges')), ]; } return ['inmate' => $demographics ? field_list($xp, $demographics) : [], 'bookings' => $bookings]; } function line(array $fields): void { echo implode('|', array_map('clean_field', $fields)), "\n"; } function print_inmate(array $inmate): void { foreach ($inmate['inmate'] as $label => $value) { line(['inmate', $label, $value]); } foreach ($inmate['bookings'] as $booking) { foreach ($booking['fields'] as $label => $value) { line(['booking', $label, $value]); } foreach ($booking['bonds'] as $bond) { line(array_merge(['bond'], $bond)); } foreach ($booking['courts'] as $court) { line(array_merge(['court'], $court)); } foreach ($booking['charges'] as $charge) { line(array_merge(['charge'], $charge)); } } } $opts = getopt('', ['last:', 'first:', 'verbose']); $inmates = array_map(fn($url) => parse_inmate(fetch($url)), find_inmates(JAIL_URL, $opts['last'] ?? fail('Missing --last'), $opts['first'] ?? '')); if (isset($opts['verbose'])) { echo json_encode($inmates, JSON_PRETTY_PRINT), "\n"; } else { array_map('print_inmate', $inmates); }