<?php
// PHP 7.4+ com a extensão curl. Registra o Request-Id de cada chamada.
const BASE_URL = 'https://api.zouti.com.br';
function zouti_get(string $path): array
{
$headers = [];
$ch = curl_init(BASE_URL . $path);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('ZOUTI_API_KEY')],
CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$headers) {
$parts = explode(':', $line, 2);
if (count($parts) === 2) {
$headers[strtolower(trim($parts[0]))] = trim($parts[1]);
}
return strlen($line);
},
]);
$raw = curl_exec($ch);
if ($raw === false) {
throw new RuntimeException(curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$body = json_decode($raw, true);
printf("GET %s → HTTP %d · Request-Id %s\n", $path, $status, $headers['request-id'] ?? '-');
if ($status !== 200) {
// error.request_id é o mesmo valor do header.
$error = $body['error'];
printf("%s: %s (request_id %s)\n", $error['code'], $error['message'], $error['request_id']);
}
return $body;
}
zouti_get('/v1/orders?limit=1');
zouti_get('/v1/orders/ord_inexistente');