-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
123 lines (106 loc) · 4.13 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
// Load variables from the .env file
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$apiKey = $_ENV['API_KEY'];
$apiBaseUrl = 'https://cinestream-api.p.rapidapi.com';
// Helper function to make requests to the API
function fetchAPI($endpoint, $params = []) {
global $apiKey, $apiBaseUrl;
$url = $apiBaseUrl . $endpoint;
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-RapidAPI-Key: ' . $apiKey
],
]);
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
// Capture the query from the URL
$query = isset($_GET['query']) ? $_GET['query'] : '';
// If there is a query, perform the search
if ($query) {
$movies = fetchAPI('/search/movie', ['query' => $query]);
} else {
// Otherwise, show popular movies
$movies = fetchAPI('/movie/popular');
}
// Sort movies by IMDb rating (in descending order)
usort($movies, function($a, $b) {
return $b['rating'] - $a['rating'];
});
// Function to limit the description
function limitDescription($description, $limit = 150) {
if (strlen($description) > $limit) {
return substr($description, 0, $limit) . '...';
}
return $description;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Movie Search</title>
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<link rel="stylesheet" href="css/search.css">
</head>
<body>
<!-- Responsive Header -->
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="index.php">
<img src="images/logo.png" alt="Logo">
<span class="brand-name">CineStream</span>
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarContent" aria-controls="navbarContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
<div class="container">
<h1 class="text-center my-5">Search for Movies</h1>
<!-- Search Bar -->
<div class="mb-4">
<form action="search.php" method="get" class="search-container">
<input type="text" class="form-control" name="query" placeholder="Search for a movie..." value="<?= htmlspecialchars($query) ?>">
<button type="submit">
<i class="fas fa-search"></i>
</button>
</form>
</div>
<!-- Movie List -->
<div id="moviesContainer" class="movie-list">
<?php if (!empty($movies)): ?>
<?php foreach ($movies as $movie): ?>
<div class="movie-list-item d-flex">
<img src="<?= $movie['poster'] ?>" alt="<?= $movie['title'] ?>">
<div>
<h5>
<a href="movie-details.php?movie_id=<?= $movie['tmdb_id'] ?>" class="text-white"><?= $movie['title'] ?></a>
</h5>
<p><?= limitDescription($movie['description'], 150) ?></p>
<p>Rating: <?= $movie['rating'] ?></p>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>No movies found for your search.</p>
<?php endif; ?>
</div>
</div>
<!-- JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
</body>
</html>