Actualiser api.php

This commit is contained in:
2026-07-02 11:11:06 +02:00
parent 35132e8680
commit ce5b41d67a
+35 -14
View File
@@ -116,26 +116,47 @@ function emptyPhysicalResult() {
]; ];
} }
// ── SCRAPPING FNAC ── // ── SCRAPPING FNAC (CORRIGÉ) ──
function fetchFromFnac($ean) { function fetchFromFnac($ean) {
$empty = emptyPhysicalResult(); $empty = emptyPhysicalResult();
$url = "https://www.fnac.com/SearchResult/ResultList.aspx?Search=" . urlencode($ean); $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; if (!$html) return $empty;
// Extraction du titre du premier résultat // ✅ CORRECTION : On vérifie si on a bien été redirigé vers une page produit
if (preg_match('/<a[^>]*class="[^"]*js-ProductTitle[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) { // Si l'URL finale contient encore "SearchResult", c'est qu'on est sur la page de recherche
$empty['title'] = trim($m[1]); // (avec les bannières sponsorisées type "Project Hail Mary"). On ignore pour éviter les pubs.
} elseif (preg_match('/<h2[^>]*class="[^"]*title[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) { if (strpos($finalUrl, 'SearchResult/ResultList.aspx') === false && strpos($finalUrl, 'SearchResult') === false) {
$empty['title'] = trim($m[1]);
} // On est sur la page produit ! On récupère le titre via og:title (infaillible)
if (preg_match('/<meta[^>]*property="og:title"[^>]*content="([^"]+)"/i', $html, $m)) {
// Extraction de l'image $title = html_entity_decode(trim($m[1]), ENT_QUOTES | ENT_HTML5, 'UTF-8');
if (preg_match('/<img[^>]*class="[^"]*js-ProductImage[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) { // Nettoyage du titre FNAC qui peut contenir " | Fnac" à la fin
$empty['poster'] = $m[1]; $title = preg_replace('/\s*\|\s*Fnac.*$/i', '', $title);
} elseif (preg_match('/<img[^>]*class="[^"]*product-image[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) { $empty['title'] = trim($title);
$empty['poster'] = $m[1]; } elseif (preg_match('/<h1[^>]*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('/<meta[^>]*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'])) { if (!empty($empty['title'])) {
$empty['format'] = detectFormat($empty['title']); $empty['format'] = detectFormat($empty['title']);