getConnection(); $stmt = $pdo->query("SELECT DISTINCT idclient FROM datadb WHERE idclient IS NOT NULL AND idclient > 0 ORDER BY idclient ASC"); $ids = $stmt->fetchAll(PDO::FETCH_COLUMN); $fakeClients = array_map(fn($id) => [ 'IdCliente' => (int) $id, 'Nominativo' => "Cliente Simulato {$id}", 'CodiceCliente' => "SIM_{$id}", ], $ids); echo json_encode(['value' => $fakeClients]); exit; } // Parametri OData $params = [ '$select' => 'IdCliente,Nominativo,CodiceCliente', '$orderby' => 'Nominativo asc' ]; // Costruisce query string con encoding corretto $queryString = http_build_query($params); // Componi endpoint finale $endpoint = "Cliente?$queryString"; // Funzione per eseguire la chiamata con retry function makeApiRequest($api, $endpoint, $maxRetries = 3) { for ($retry = 0; $retry < $maxRetries; $retry++) { try { // Tenta la chiamata API $data = $api->get($endpoint); // Salva risposta per debug file_put_contents(__DIR__ . '/clienti_response.json', json_encode($data, JSON_PRETTY_PRINT)); return $data; } catch (Exception $e) { $errorMessage = $e->getMessage(); // Controlla se l'errore รจ legato all'autenticazione (HTTP 400 con messaggio specifico) if (strpos($errorMessage, 'HTTP 400') !== false && strpos($errorMessage, 'Cannot persist the object') !== false) { // Forza il refresh del token try { // Assumi che VisualLimsApiClient abbia un metodo per il refresh del token $api->refreshToken(); // Da implementare in VisualLimsApiClient se non esiste error_log("Tentativo $retry: Refresh token eseguito per endpoint $endpoint"); } catch (Exception $refreshEx) { error_log("Errore durante il refresh del token: " . $refreshEx->getMessage()); throw new Exception("Impossibile eseguire il refresh del token: " . $refreshEx->getMessage()); } // Ritarda leggermente prima del retry usleep(500000); // 500ms continue; } // Altri errori non gestiti dal retry throw $e; } } throw new Exception("Massimo numero di tentativi raggiunto per $endpoint"); } // Esegui la chiamata con retry $data = makeApiRequest($api, $endpoint); echo json_encode($data); } catch (Exception $e) { http_response_code(500); $errorResponse = [ 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTraceAsString() ]; error_log("Errore in get_clienti.php: " . json_encode($errorResponse)); echo json_encode($errorResponse); }