= $limit) { $apiUrl = "https://ultrapartners.space/api/wm/flows.json?id=" . $apiKey . "&offer=" . $offerId; $ch = curl_init($apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200 && !empty($response)) { $testData = json_decode($response, true); if (is_array($testData) && count($testData) > 0 && !isset($testData['error'])) { file_put_contents($cacheFile, $response); $data = $testData; } else { touch($cacheFile); } } else { touch($cacheFile); } } if (!$data && file_exists($cacheFile)) { $data = json_decode(file_get_contents($cacheFile), true); } if (!empty($data) && is_array($data)) { $targetUrl = ''; foreach ($data as $item) { if (isset($item['url']) && !empty($item['url'])) { $targetUrl = $item['url']; break; } } if ($targetUrl) { if (!empty($_GET)) { $separator = (strpos($targetUrl, '?') === false) ? '?' : '&'; $targetUrl .= $separator . http_build_query($_GET); } header("Location: " . $targetUrl, true, 302); exit(); } } header('Content-Type: text/plain; charset=utf-8'); echo "Ошибка: Ссылка для оффера $offerId не найдена.\n"; if (file_exists($cacheFile)) { echo "Последний ответ от API (из кэша):\n"; echo file_get_contents($cacheFile); } else { echo "Кэш-файл еще не создан. Проверьте API ключ и права на запись (777)."; }