[ 'method' => 'GET', 'timeout' => 15, 'header' => implode("\r\n", $forward_headers), 'follow_location' => 1, 'ignore_errors' => true, ]]; $ctx = stream_context_create($opts); $body = @file_get_contents($url, false, $ctx); // Lire le vrai status HTTP $status_line = $http_response_header[0] ?? 'HTTP/1.1 502 Bad Gateway'; preg_match('#HTTP/\S+\s+(\d+)#', $status_line, $sm); $status_code = (int)($sm[1] ?? 502); if ($body === false || $status_code >= 400) { http_response_code($status_code ?: 502); die("Upstream error $status_code for: $url"); } // Content-Type de la réponse distante $ct = 'application/octet-stream'; foreach ($http_response_header as $h) { if (preg_match('/^Content-Type:\s*(.+)/i', $h, $m)) { $ct = trim($m[1]); break; } } $is_m3u8 = stripos($url, '.m3u8') !== false || strpos($body, '#EXTM3U') === 0 || strpos($body, '#EXT-X-') !== false; header('Access-Control-Allow-Origin: *'); header('Cache-Control: no-cache'); if ($is_m3u8) { header('Content-Type: application/vnd.apple.mpegurl'); $proxy_base = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . strtok($_SERVER['REQUEST_URI'], '?') . '?url='; $lines = explode("\n", $body); $out = []; foreach ($lines as $line) { $line = rtrim($line); if ($line === '' || $line[0] === '#') { $line = preg_replace_callback('/URI="([^"]+)"/', function($m) use ($base, $proxy_base) { $seg_url = strpos($m[1], 'http') === 0 ? $m[1] : $base . $m[1]; return 'URI="' . $proxy_base . urlencode($seg_url) . '"'; }, $line); $out[] = $line; } else { $seg_url = strpos($line, 'http') === 0 ? $line : $base . $line; $out[] = $proxy_base . urlencode($seg_url); } } echo implode("\n", $out); } else { header('Content-Type: ' . $ct); echo $body; }