From 6a1a0940870c69058326a6c046fc4f35ba2b5062 Mon Sep 17 00:00:00 2001 From: Cedric Date: Wed, 24 Jun 2026 13:53:18 +0200 Subject: [PATCH] Actualiser api.php --- api.php | 101 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 15 deletions(-) diff --git a/api.php b/api.php index a2a8aef..bcaa362 100644 --- a/api.php +++ b/api.php @@ -105,60 +105,129 @@ function extractYear($dateStr) { return ''; } -// ── 1. API DVDFr (SANS CACHE) ── function fetchDVDFr($ean, $pdo) { - if (empty($ean) || strlen($ean) < 8) return null; + if (empty($ean) || strlen($ean) < 8) { + error_log("DVDFr: EAN invalide ou vide - '$ean'"); + return null; + } $ua = 'MonCinema/1.0 (collection privée; contact@moncineapp.fr)'; + // Étape 1 : recherche par gencode $searchUrl = "https://www.dvdfr.com/api/search.php?gencode=" . urlencode($ean); + error_log("DVDFr: Appel API search - URL: $searchUrl"); + $ch = curl_init($searchUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => 5, - CURLOPT_CONNECTTIMEOUT => 3, + CURLOPT_TIMEOUT => 8, + CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => $ua, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTPHEADER => ['Accept: application/xml, text/xml, */*'], ]); $res = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); - if (!$res) return null; + + if (!$res) { + error_log("DVDFr: Échec de la requête search - HTTP $httpCode"); + return null; + } + + error_log("DVDFr: Réponse search reçue - " . substr($res, 0, 200)); libxml_use_internal_errors(true); $xml = simplexml_load_string($res); + $xmlErrors = libxml_get_errors(); libxml_clear_errors(); - if (!$xml || !isset($xml->dvd[0]->id)) return null; - $dvdId = (string)$xml->dvd[0]->id; - if (empty($dvdId)) return null; + if (!$xml) { + error_log("DVDFr: Impossible de parser le XML search - Erreurs: " . json_encode($xmlErrors)); + return null; + } + // 🔥 Meilleure détection de la structure XML + $dvdId = null; + + if (isset($xml->dvd) && count($xml->dvd) > 0) { + $dvdId = (string)$xml->dvd[0]->id; + } elseif (isset($xml->resultats) && isset($xml->resultats->dvd)) { + $dvdId = (string)$xml->resultats->dvd[0]->id; + } elseif (isset($xml->results) && count($xml->results) > 0) { + $dvdId = (string)$xml->results[0]->id; + } + + if (empty($dvdId)) { + error_log("DVDFr: Aucun ID DVD trouvé dans la réponse search"); + error_log("DVDFr: Structure XML: " . json_encode($xml)); + return null; + } + + error_log("DVDFr: ID DVD trouvé - $dvdId"); + + // Étape 2 : fiche complète $ficheUrl = "https://www.dvdfr.com/api/dvd.php?id=" . urlencode($dvdId); + error_log("DVDFr: Appel API fiche - URL: $ficheUrl"); + $ch2 = curl_init($ficheUrl); curl_setopt_array($ch2, [ CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => 5, - CURLOPT_CONNECTTIMEOUT => 3, + CURLOPT_TIMEOUT => 8, + CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => $ua, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTPHEADER => ['Accept: application/xml, text/xml, */*'], ]); $res2 = curl_exec($ch2); + $httpCode2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE); curl_close($ch2); - if (!$res2) return null; + + if (!$res2) { + error_log("DVDFr: Échec de la requête fiche - HTTP $httpCode2"); + return null; + } + + error_log("DVDFr: Réponse fiche reçue - " . substr($res2, 0, 300)); libxml_use_internal_errors(true); $fiche = simplexml_load_string($res2); + $ficheErrors = libxml_get_errors(); libxml_clear_errors(); - if (!$fiche || !isset($fiche->dvd[0])) return null; - $dvd = $fiche->dvd[0]; + if (!$fiche) { + error_log("DVDFr: Impossible de parser le XML fiche - Erreurs: " . json_encode($ficheErrors)); + return null; + } + // 🔥 Meilleure détection de la fiche DVD + $dvd = null; + + if (isset($fiche->dvd) && count($fiche->dvd) > 0) { + $dvd = $fiche->dvd[0]; + } elseif (isset($fiche->fiche)) { + $dvd = $fiche->fiche; + } elseif (isset($fiche->product)) { + $dvd = $fiche->product; + } + + if (!$dvd) { + error_log("DVDFr: Structure de fiche non reconnue"); + error_log("DVDFr: Structure XML fiche: " . json_encode($fiche)); + return null; + } + + // Extraction des données avec fallbacks $poster = ''; - if (isset($dvd->cover)) $poster = (string)$dvd->cover; - if (empty($poster) && isset($dvd->covers->cover[0])) $poster = (string)$dvd->covers->cover[0]; + if (isset($dvd->cover)) { + $poster = (string)$dvd->cover; + } elseif (isset($dvd->covers) && isset($dvd->covers->cover)) { + $poster = (string)$dvd->covers->cover[0]; + } elseif (isset($dvd->image)) { + $poster = (string)$dvd->image; + } $result = [ 'poster' => $poster, @@ -169,6 +238,8 @@ function fetchDVDFr($ean, $pdo) { 'discs' => isset($dvd->nbdisques) ? (string)$dvd->nbdisques : '', ]; + error_log("DVDFr: Données extraites - " . json_encode($result)); + return (!empty($result['poster']) || !empty($result['publisher'])) ? $result : null; }