62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
// controllers/TmdbController.php
|
|
// Proxy serveur vers TMDB : la clé API ne quitte jamais le backend.
|
|
|
|
class TmdbController {
|
|
private $apiKey;
|
|
private $baseUrl = 'https://api.themoviedb.org/3';
|
|
|
|
public function __construct($apiKey) {
|
|
$this->apiKey = $apiKey;
|
|
}
|
|
|
|
private function fetch($endpoint, $params = []) {
|
|
$params['api_key'] = $this->apiKey;
|
|
$url = $this->baseUrl . $endpoint . '?' . http_build_query($params);
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE) ?: 502;
|
|
curl_close($ch);
|
|
|
|
return ['code' => $httpCode, 'body' => $response ?: json_encode(["error" => "TMDB injoignable"])];
|
|
}
|
|
|
|
public function search($data) {
|
|
$query = trim($data['query'] ?? '');
|
|
if ($query === '') {
|
|
http_response_code(400);
|
|
echo json_encode(["error" => "Paramètre 'query' manquant"]);
|
|
return;
|
|
}
|
|
|
|
$result = $this->fetch('/search/movie', [
|
|
'query' => $query,
|
|
'language' => 'fr-FR',
|
|
'page' => 1
|
|
]);
|
|
|
|
http_response_code($result['code']);
|
|
echo $result['body'];
|
|
}
|
|
|
|
public function details($data) {
|
|
$id = $data['id'] ?? null;
|
|
if (!$id || !ctype_digit((string)$id)) {
|
|
http_response_code(400);
|
|
echo json_encode(["error" => "ID de film invalide"]);
|
|
return;
|
|
}
|
|
|
|
$result = $this->fetch("/movie/{$id}", [
|
|
'language' => 'fr-FR',
|
|
'append_to_response' => 'credits'
|
|
]);
|
|
|
|
http_response_code($result['code']);
|
|
echo $result['body'];
|
|
}
|
|
}
|