diff --git a/api.php b/api.php index c12b7a5..af66980 100644 --- a/api.php +++ b/api.php @@ -116,26 +116,47 @@ function emptyPhysicalResult() { ]; } -// ── SCRAPPING FNAC ── +// ── SCRAPPING FNAC (CORRIGÉ) ── function fetchFromFnac($ean) { $empty = emptyPhysicalResult(); $url = "https://www.fnac.com/SearchResult/ResultList.aspx?Search=" . urlencode($ean); - $html = httpGet($url, 10); + + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 10, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_FOLLOWLOCATION => true, // Important pour suivre la redirection vers la page produit + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0', + ]); + $html = curl_exec($ch); + $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // Récupère l'URL après redirection + curl_close($ch); + if (!$html) return $empty; - // Extraction du titre du premier résultat - if (preg_match('/]*class="[^"]*js-ProductTitle[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) { - $empty['title'] = trim($m[1]); - } elseif (preg_match('/]*class="[^"]*title[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) { - $empty['title'] = trim($m[1]); - } - - // Extraction de l'image - if (preg_match('/]*class="[^"]*js-ProductImage[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) { - $empty['poster'] = $m[1]; - } elseif (preg_match('/]*class="[^"]*product-image[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) { - $empty['poster'] = $m[1]; + // ✅ CORRECTION : On vérifie si on a bien été redirigé vers une page produit + // Si l'URL finale contient encore "SearchResult", c'est qu'on est sur la page de recherche + // (avec les bannières sponsorisées type "Project Hail Mary"). On ignore pour éviter les pubs. + if (strpos($finalUrl, 'SearchResult/ResultList.aspx') === false && strpos($finalUrl, 'SearchResult') === false) { + + // On est sur la page produit ! On récupère le titre via og:title (infaillible) + if (preg_match('/]*property="og:title"[^>]*content="([^"]+)"/i', $html, $m)) { + $title = html_entity_decode(trim($m[1]), ENT_QUOTES | ENT_HTML5, 'UTF-8'); + // Nettoyage du titre FNAC qui peut contenir " | Fnac" à la fin + $title = preg_replace('/\s*\|\s*Fnac.*$/i', '', $title); + $empty['title'] = trim($title); + } elseif (preg_match('/]*class="[^"]*f-product__name[^"]*"[^>]*>([^<]+)<\/h1>/i', $html, $m)) { + $empty['title'] = trim(strip_tags($m[1])); + } + + // Récupération de l'image via og:image + if (preg_match('/]*property="og:image"[^>]*content="([^"]+)"/i', $html, $m)) { + $empty['poster'] = trim($m[1]); + } } + // Si on est resté sur la page de recherche, on retourne un tableau vide. + // Le fallback Blu-ray.com prendra le relais proprement. if (!empty($empty['title'])) { $empty['format'] = detectFormat($empty['title']);