'Parametro "field" mancante']); exit; } $api = VisualLimsApiClient::getInstance(); // also loads dotenv as a side-effect $simulate = ($_ENV['SIMULATE_EXPORT_LIMS'] ?? '') === 'true'; $base_url = 'https://93.43.5.102/limsapi/api/odata/'; $data = null; $endpoint = null; $cache_file = null; $options = []; // qui puoi aggiungere filtri/ordering per campo se serve try { switch ($field) { case 'MoltiplicatorePrezzo': $endpoint = 'MoltiplicatorePrezzi'; $cache_file = __DIR__ . '/cache/moltiplicatori_prezzo.json'; break; case 'AnagraficaCertestObject': $endpoint = 'AnagraficaCertestObject'; $cache_file = __DIR__ . '/cache/anagrafica_certest_object.json'; break; case 'AnagraficaCertestService': $endpoint = 'AnagraficaCertestService'; $cache_file = __DIR__ . '/cache/anagrafica_certest_service.json'; break; case 'ClienteResponsabile': $id_cliente = (int)($_GET['id_cliente'] ?? 0); if ($id_cliente <= 0) { if (!$simulate) { http_response_code(400); echo json_encode(['error' => 'Manca o invalido id_cliente']); exit; } $id_cliente = 1; // dummy — mock ignores the actual ID } $endpoint = "Cliente($id_cliente)?\$expand=Responsabili"; $cache_file = __DIR__ . '/cache/cliente_responsabili_' . $id_cliente . '.json'; break; // case 'FuturoCampo5': // $endpoint = 'QualcosaElse'; // break; default: http_response_code(400); echo json_encode(['error' => "Campo non supportato: $field"]); exit; } // Caching skipped in simulate mode to avoid polluting real-data cache files if (!$simulate && $cache_file && file_exists($cache_file) && (time() - filemtime($cache_file) < 3600)) { // 1 ora $data = json_decode(file_get_contents($cache_file), true); } else { $data = $api->get($endpoint, $options); if (!$simulate && $cache_file) { file_put_contents($cache_file, json_encode($data, JSON_PRETTY_PRINT)); } } // Log ultimo URL (opzionale, per debug) $full_url = $base_url . $endpoint . ($options ? '?' . http_build_query($options) : ''); file_put_contents(__DIR__ . '/last_urls.log', date('c') . " - $field - $full_url\n", FILE_APPEND); echo json_encode($data); } catch (Exception $e) { file_put_contents( __DIR__ . '/error_log.txt', date('Y-m-d H:i:s') . " [$field] " . $e->getMessage() . PHP_EOL, FILE_APPEND ); http_response_code(500); echo json_encode(['error' => $e->getMessage()]); }