'; $apiKey = getenv('CODEB_API_KEY'); $date = getenv('DATE') ?: date('Y-m-d', strtotime('-1 day')); $out = getenv('OUT_FILE') ?: "cdr-$date.csv"; if (!$apiKey) { fwrite(STDERR, "CODEB_API_KEY not set\n"); exit(2); } $cols = ['recordedUtc','startUtc','endUtc','direction','trunk', 'callingNumber','calledNumber','durationSec','answered','outcome','releaseReason']; $ch = curl_init("$tenant/signal.ashx?cdrlog=1&date=$date&limit=5000"); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $apiKey], CURLOPT_TIMEOUT => 30, ]); $body = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($body === false || $code >= 400) { fwrite(STDERR, "HTTP $code\n"); exit(1); } $envelope = json_decode($body, true); $events = $envelope['events'] ?? []; $fp = fopen($out, 'w'); fputcsv($fp, $cols); foreach ($events as $e) { $row = []; foreach ($cols as $c) $row[] = $e[$c] ?? ''; fputcsv($fp, $row); } fclose($fp); echo "[done] ", count($events), " rows -> $out\n";