'IdCliente,Nominativo,CodiceCliente', '$orderby' => 'Nominativo asc' ]; $data = $api->get("Cliente?" . http_build_query($params)); if (!is_dir(__DIR__ . '/cache')) mkdir(__DIR__ . '/cache', 0777, true); file_put_contents($cacheFile, json_encode($data)); } $clients = $data['value'] ?? []; // If requesting by specific ID (for loading selected value) if ($id !== null) { foreach ($clients as $c) { if ((int)$c['IdCliente'] === $id) { echo json_encode(['results' => [['id' => $c['IdCliente'], 'text' => trim($c['Nominativo'] ?? '')]]]); exit; } } echo json_encode(['results' => []]); exit; } // Search by query $results = []; foreach ($clients as $c) { $name = trim($c['Nominativo'] ?? ''); $code = trim($c['CodiceCliente'] ?? ''); if ($q === '' || mb_strpos(mb_strtolower($name), $q) !== false || mb_strpos(mb_strtolower($code), $q) !== false) { $results[] = ['id' => $c['IdCliente'], 'text' => $name]; if (count($results) >= $limit) break; } } echo json_encode(['results' => $results]); } catch (Exception $e) { http_response_code(500); echo json_encode(['error' => $e->getMessage()]); }