Actualiser api.php

This commit is contained in:
2026-06-24 15:48:44 +02:00
parent 59d3f1261d
commit 70f04af36b
+113 -26
View File
@@ -670,42 +670,129 @@ switch ($action) {
break; break;
case 'debug_dvdfr': case 'debug_dvdfr':
checkAuth($pdo); // 🔥 DEBUG SANS AUTHENTIFICATION (à supprimer après test)
$ean = $_GET['ean'] ?? ''; $ean = $_GET['ean'] ?? '';
if (!$ean) { echo json_encode(['error' => 'EAN manquant']); exit; } if (!$ean) { echo json_encode(['error' => 'EAN manquant']); exit; }
$ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0.0.0 Safari/537.36'; $result = ['ean' => $ean, 'steps' => []];
// Étape 1 : page de recherche // Étape 1 : Recherche
$ch = curl_init('https://www.dvdfr.com/search/?q=' . urlencode($ean)); $ua = 'MonCinema/1.0 (collection privée; contact@moncineapp.fr)';
$searchUrl = "https://www.dvdfr.com/api/search.php?gencode=" . urlencode($ean);
$result['steps']['search_url'] = $searchUrl;
$ch = curl_init($searchUrl);
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERAGENT => $ua, CURLOPT_TIMEOUT => 8,
CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => '', CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_HTTPHEADER => ['Accept-Language: fr-FR,fr;q=0.9'], CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => $ua,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTPHEADER => ['Accept: application/xml, text/xml, */*'],
]); ]);
$searchHtml = curl_exec($ch); $res = curl_exec($ch);
$searchCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$searchFinal = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $curlError = curl_error($ch);
curl_close($ch); curl_close($ch);
// Extraire tous les liens .html $result['steps']['search'] = [
preg_match_all('@href=["\']([^"\']+\.html)["\']@i', $searchHtml, $links); 'http_code' => $httpCode,
$allLinks = array_unique($links[1]); 'curl_error' => $curlError,
'response_length' => strlen($res ?: ''),
'response_preview' => substr($res ?: '', 0, 500)
];
// Chercher og:image dans la page de recherche if (!$res || $httpCode !== 200) {
preg_match('/<meta[^>]+property=["\']og:image["\'][^>]+content=["\']([^"\']+)["\']/i', $searchHtml, $ogSearch); $result['error'] = "Échec recherche HTTP $httpCode";
echo json_encode($result);
exit;
}
// Chercher toutes les <img> // Parser XML
preg_match_all('/<img[^>]+src=["\']([^"\']+)["\'][^>]*>/i', $searchHtml, $allImgs); libxml_use_internal_errors(true);
$xml = simplexml_load_string($res);
$xmlErrors = libxml_get_errors();
libxml_clear_errors();
echo json_encode([ if (!$xml) {
'search_http_code' => $searchCode, $result['error'] = "XML invalide";
'search_final_url' => $searchFinal, $result['xml_errors'] = $xmlErrors;
'search_og_image' => $ogSearch[1] ?? null, echo json_encode($result);
'search_links_html' => array_slice($allLinks, 0, 20), exit;
'search_imgs' => array_slice($allImgs[1], 0, 15), }
'search_html_sample' => substr($searchHtml, 0, 3000),
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); $result['steps']['xml_structure'] = json_decode(json_encode($xml), true);
if (!isset($xml->dvd[0]->id)) {
$result['error'] = "Pas de DVD trouvé dans la réponse";
echo json_encode($result);
exit;
}
$dvdId = (string)$xml->dvd[0]->id;
$result['steps']['dvd_id'] = $dvdId;
// Étape 2 : Fiche complète
$ficheUrl = "https://www.dvdfr.com/api/dvd.php?id=" . urlencode($dvdId);
$result['steps']['fiche_url'] = $ficheUrl;
$ch2 = curl_init($ficheUrl);
curl_setopt_array($ch2, [
CURLOPT_RETURNTRANSFER => true,
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);
$curlError2 = curl_error($ch2);
curl_close($ch2);
$result['steps']['fiche'] = [
'http_code' => $httpCode2,
'curl_error' => $curlError2,
'response_length' => strlen($res2 ?: ''),
'response_preview' => substr($res2 ?: '', 0, 1000)
];
if (!$res2 || $httpCode2 !== 200) {
$result['error'] = "Échec fiche HTTP $httpCode2";
echo json_encode($result);
exit;
}
libxml_use_internal_errors(true);
$fiche = simplexml_load_string($res2);
libxml_clear_errors();
if (!$fiche || !isset($fiche->dvd[0])) {
$result['error'] = "Fiche XML invalide";
echo json_encode($result);
exit;
}
$dvd = $fiche->dvd[0];
$result['steps']['fiche_structure'] = json_decode(json_encode($dvd), true);
// Extraction finale
$poster = '';
if (isset($dvd->cover)) $poster = (string)$dvd->cover;
if (empty($poster) && isset($dvd->covers->cover[0])) $poster = (string)$dvd->covers->cover[0];
$result['final'] = [
'poster' => $poster,
'publisher' => isset($dvd->editeur) ? (string)$dvd->editeur : '',
'format' => isset($dvd->type) ? (string)$dvd->type : '',
'length' => isset($dvd->duree) ? (string)$dvd->duree : '',
'aspect' => isset($dvd->formatimage) ? (string)$dvd->formatimage : '',
'discs' => isset($dvd->nbdisques) ? (string)$dvd->nbdisques : '',
];
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;
break; break;
} }