|null, raw: string} */ function sendpier_get(string $path): array { $key = getenv('SENDPIER_API_KEY'); $domain = getenv('SENDPIER_DOMAIN'); if ($key === false || $key === '' || $domain === false || $domain === '') { fwrite(STDERR, "set SENDPIER_API_KEY and SENDPIER_DOMAIN first\n"); exit(78); } $handle = curl_init(API_BASE.$path); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle, CURLOPT_TIMEOUT, 30); curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Authorization: Bearer '.$key, 'X-Domain-Name: '.$domain, 'Accept: application/json', ]); $raw = curl_exec($handle); if ($raw === false) { $error = curl_error($handle); curl_close($handle); fwrite(STDERR, "connection failed: {$error}\n"); exit(1); } $status = (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE); curl_close($handle); $decoded = json_decode((string) $raw, true); return [ 'status' => $status, 'body' => is_array($decoded) ? $decoded : null, 'raw' => (string) $raw, ]; } $response = sendpier_get('/v1/quota'); echo $response['raw'], "\n"; if ($response['status'] !== 200) { fwrite(STDERR, "request failed with HTTP {$response['status']}\n"); exit(1); } $quota = $response['body'] ?? []; fwrite(STDERR, sprintf( "%s: %d of %d used, %d left this period\n", (string) ($quota['domain'] ?? '?'), (int) ($quota['used_this_month'] ?? 0), (int) ($quota['monthly_limit'] ?? 0), (int) ($quota['remaining'] ?? 0) )); // A pause before you run out is cheaper than a batch that dies halfway. if ((int) ($quota['remaining'] ?? 0) < 100) { fwrite(STDERR, "warning: fewer than 100 messages left\n"); }