Actualiser api.php
This commit is contained in:
@@ -116,41 +116,52 @@ function emptyPhysicalResult() {
|
||||
];
|
||||
}
|
||||
|
||||
// ── SCRAPPING FNAC (CORRECTION RADICALE) ──
|
||||
// ── SCRAPPING FNAC (CORRECTION v2 : vérification EAN anti-faux-positif) ──
|
||||
function fetchFromFnac($ean) {
|
||||
$empty = emptyPhysicalResult();
|
||||
$url = "https://www.fnac.com/SearchResult/ResultList.aspx?Search=" . urlencode($ean);
|
||||
$html = httpGet($url, 15);
|
||||
if (!$html) return $empty;
|
||||
|
||||
// ✅ CORRECTION : On ignore totalement le JSON-LD qui est pollué par les bannières publicitaires (type "Project Hail Mary").
|
||||
// On cherche spécifiquement le premier lien HTML qui pointe vers une vraie fiche produit FNAC.
|
||||
// Les URLs de produits FNAC contiennent "/tp12345", "/A12345" ou "-s123.html".
|
||||
|
||||
// 🔍 LOG DE DIAGNOSTIC — à garder tant que le bug n'est pas confirmé résolu.
|
||||
// Permet de vérifier si FNAC renvoie vraiment une page de résultats
|
||||
// ou une page anti-bot / fallback générique (souvent la vraie cause
|
||||
// d'un titre "aléatoire" du type "Project Hail Mary").
|
||||
@file_put_contents(
|
||||
__DIR__ . '/debug_fnac_' . preg_replace('/[^0-9]/', '', $ean) . '.html',
|
||||
$html
|
||||
);
|
||||
|
||||
// ✅ GARDE-FOU : si l'EAN recherché n'apparaît nulle part dans le HTML
|
||||
// retourné, ce n'est pas une vraie page de résultats pour cet EAN
|
||||
// (bannière promo, page anti-bot, page d'accueil de secours...).
|
||||
// On refuse le résultat plutôt que de propager un titre erroné.
|
||||
$eanDigits = preg_replace('/[^0-9]/', '', (string)$ean);
|
||||
if ($eanDigits && strpos($html, $eanDigits) === false) {
|
||||
error_log("fetchFromFnac: EAN $eanDigits absent du HTML retourné — page rejetée (probable anti-bot/fallback).");
|
||||
return $empty;
|
||||
}
|
||||
|
||||
// 1. Chercher un lien avec un titre ou du texte interne
|
||||
if (preg_match('/<a[^>]*href="\/(?:[^"]*\/)?(?:tp\d+|A\d+|[^"]*-s\d+\.html)"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
}
|
||||
// 2. Chercher un lien avec un attribut title
|
||||
}
|
||||
elseif (preg_match('/<a[^>]*href="\/(?:[^"]*\/)?(?:tp\d+|A\d+|[^"]*-s\d+\.html)"[^>]*title="([^"]+)"[^>]*>/i', $html, $m)) {
|
||||
$empty['title'] = trim($m[1]);
|
||||
}
|
||||
// 3. Fallback sur les classes CSS courantes
|
||||
}
|
||||
elseif (preg_match('/<h2[^>]*class="[^"]*f-product__name[^"]*"[^>]*>([^<]+)<\/h2>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
}
|
||||
}
|
||||
elseif (preg_match('/<a[^>]*class="[^"]*js-ProductTitle[^"]*"[^>]*>([^<]+)<\/a>/i', $html, $m)) {
|
||||
$empty['title'] = trim(strip_tags($m[1]));
|
||||
}
|
||||
|
||||
// Nettoyage du titre
|
||||
if (!empty($empty['title'])) {
|
||||
$empty['title'] = html_entity_decode($empty['title'], ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$empty['title'] = preg_replace('/\s*\|\s*Fnac.*$/i', '', $empty['title']);
|
||||
$empty['title'] = trim($empty['title']);
|
||||
}
|
||||
|
||||
// Récupération de l'image
|
||||
if (preg_match('/<meta[^>]*property="og:image"[^>]*content="([^"]+)"/i', $html, $m)) {
|
||||
$empty['poster'] = trim($m[1]);
|
||||
} elseif (preg_match('/<img[^>]*class="[^"]*js-ProductImage[^"]*"[^>]*src="([^"]+)"/i', $html, $m)) {
|
||||
@@ -160,7 +171,7 @@ function fetchFromFnac($ean) {
|
||||
if (!empty($empty['title'])) {
|
||||
$empty['format'] = detectFormat($empty['title']);
|
||||
}
|
||||
|
||||
|
||||
return $empty;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user