diff --git a/api.php b/api.php index 2ed692e..4a079e7 100644 --- a/api.php +++ b/api.php @@ -146,51 +146,77 @@ function fetchCinemaPassion($title, $year = '', $ean = '', $pdo = null) { return ['poster' => $defaultPoster, 'title' => '', 'format' => 'Blu-ray']; } - // 1. Recherche en GET (beaucoup plus fiable que le POST sur ce site) - $searchUrl = "https://www.cinemapassion.com/recherche.php?recherche=" . urlencode($cleanTitle); - $searchRes = httpGet($searchUrl, 10); + $baseUrl = 'http://www.cinemapassion.com'; - if ($searchRes) { - // 2. Extraire directement le premier lien de film - // On ne cherche plus à lire le contenu de la balise pour éviter les bugs liés au HTML interne - if (preg_match('/href=["\']?(?:\.\.\/)?(film\/[^"\'\s<>]+-\d+\.php)["\']?/i', $searchRes, $matches)) { - $filmUrl = "https://www.cinemapassion.com/" . ltrim($matches[1], './'); - $filmRes = httpGet($filmUrl, 10); - - if ($filmRes) { - // 3. Chercher la page jaquette haute définition - $jaquetteUrl = null; - if (preg_match('/href=["\']?(?:\.\.\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s<>]+\.php)["\']?/i', $filmRes, $jaqMatches)) { - $jaquetteUrl = "https://www.cinemapassion.com/" . ltrim($jaqMatches[1], './'); - $jaquetteRes = httpGet($jaquetteUrl, 10); - - if ($jaquetteRes) { - // 4. Extraire l'image covers_temp - if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s<>]+\.jpg)["\']?/i', $jaquetteRes, $imgMatches)) { - return [ - 'poster' => str_replace('http://', 'https://', $imgMatches[1]), - 'title' => $cleanTitle, - 'format' => 'Blu-ray' - ]; - } - } - } + // 1. Initialisation d'une session cURL UNIQUE (Partage des cookies) + // C'est vital car moteur2.php utilise des sessions PHP pour la recherche + $ch = curl_init(); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 5, + CURLOPT_TIMEOUT => 10, + CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', + CURLOPT_COOKIEFILE => '', // Active le stockage des cookies en RAM entre les requêtes + CURLOPT_REFERER => $baseUrl . '/', + CURLOPT_SSL_VERIFYPEER => false + ]); + + // 2. Recherche via POST + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/moteur2.php'); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['recherche' => $cleanTitle])); + $html = curl_exec($ch); + + // 3. Extraction du lien du film + $filmUrl = null; + if ($html && preg_match('/href=["\']?(?:.*?\/)?(film\/[^"\'\s>]+)["\']?/i', $html, $matches)) { + $filmUrl = $baseUrl . '/' . $matches[1]; + } else { + // Plan B : Recherche via GET si le POST échoue + curl_setopt($ch, CURLOPT_URL, $baseUrl . '/recherche.php?recherche=' . urlencode($cleanTitle)); + curl_setopt($ch, CURLOPT_POST, false); + $html = curl_exec($ch); + if ($html && preg_match('/href=["\']?(?:.*?\/)?(film\/[^"\'\s>]+)["\']?/i', $html, $matches)) { + $filmUrl = $baseUrl . '/' . $matches[1]; + } + } + + // 4. Si on a trouvé le film, on charge sa page + if ($filmUrl) { + curl_setopt($ch, CURLOPT_URL, $filmUrl); + curl_setopt($ch, CURLOPT_POST, false); + $filmHtml = curl_exec($ch); + + if ($filmHtml) { + // 5. Chercher le lien de la page contenant la jaquette + if (preg_match('/href=["\']?(?:.*?\/)?(jaquette-(?:dvd|blu-ray)-[^"\'\s>]+)["\']?/i', $filmHtml, $jaqMatches)) { + $jaqUrl = $baseUrl . '/' . $jaqMatches[1]; - // 5. Fallback : si pas de page jaquette, on cherche covers_temp sur la page principale du film - if (preg_match('/src=["\']?(https?:\/\/(?:www\.)?cinemapassion\.com\/covers_temp\/covers\d*\/[^"\'\s<>]+\.jpg)["\']?/i', $filmRes, $imgMatches)) { - if (strpos($imgMatches[1], 'miniature') === false && strpos($imgMatches[1], 'vign') === false) { - return [ - 'poster' => str_replace('http://', 'https://', $imgMatches[1]), - 'title' => $cleanTitle, - 'format' => 'Blu-ray' - ]; - } + // Charger la page de la jaquette + curl_setopt($ch, CURLOPT_URL, $jaqUrl); + $jaqHtml = curl_exec($ch); + + // 6. Extraire l'image haute définition (covers_temp/covers...) + if ($jaqHtml && preg_match('/src=["\']?(http:\/\/www\.cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $jaqHtml, $imgMatches)) { + curl_close($ch); + // On convertit l'URL en HTTPS pour éviter les erreurs de contenu mixte sur votre site web + return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; + } + } + + // Fallback : Si aucune sous-page jaquette n'existe, on cherche l'image directement sur la page du film + if (preg_match('/src=["\']?(http:\/\/www\.cinemapassion\.com\/covers_temp\/covers[^"\'\s>]+)["\']?/i', $filmHtml, $imgMatches)) { + // On s'assure qu'on ne prend pas les miniatures + if (strpos($imgMatches[1], 'miniature') === false) { + curl_close($ch); + return ['poster' => str_replace('http://', 'https://', $imgMatches[1]), 'title' => $cleanTitle, 'format' => 'Blu-ray']; } } } } - // Si la recherche échoue, on retourne l'image par défaut + curl_close($ch); return ['poster' => $defaultPoster, 'title' => $cleanTitle, 'format' => 'Blu-ray']; }