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; } // ── DEBUG MODE ─────────────────────────────────────────────────────── // Chiamata: get_clienti.php?debug=1 // Bypassa cache e $select per vedere TUTTI i campi grezzi restituiti // dall'API per l'entità Cliente. Usare solo per ispezione manuale, // NON lasciare linkato/usato in produzione dal frontend. if (($_GET['debug'] ?? '') === '1') { $debugParams = [ '$top' => 5, '$orderby' => 'Nominativo asc' // Nessun $select: chiediamo tutti i campi disponibili ]; $debugEndpoint = "Cliente?" . http_build_query($debugParams); $debugData = $api->get($debugEndpoint); $debugClients = $debugData['value'] ?? $debugData; echo json_encode([ 'mode' => 'debug - no $select, cache bypassed', 'top_level_keys' => is_array($debugData) ? array_keys($debugData) : null, 'fields_found_in_first_record' => isset($debugClients[0]) ? array_keys($debugClients[0]) : null, 'sample_records' => $debugClients, ], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); exit; } // ── FINE DEBUG MODE ────────────────────────────────────────────────── // Parametri OData $params = [ '$select' => 'IdCliente,Nominativo,CodiceCliente,Inattivo', '$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"); } // Cache file (1 hour TTL) $cacheFile = __DIR__ . '/cache/clienti.json'; if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < 3600)) { readfile($cacheFile); exit; } // Esegui la chiamata con retry $data = makeApiRequest($api, $endpoint); $json = json_encode($data); if (!is_dir(__DIR__ . '/cache')) mkdir(__DIR__ . '/cache', 0777, true); file_put_contents($cacheFile, $json); echo $json; } 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); }