forked from codewithsadee/filmlane
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
49 lines (40 loc) · 1.58 KB
/
search.php
File metadata and controls
49 lines (40 loc) · 1.58 KB
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
<?php
require_once 'api.php';
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['search'])) {
$searchQuery = $_GET['search'];
// Perform search for movies
$moviesResult = $tmdbApi->searchMovies($searchQuery);
// Perform search for TV shows
$tvShowsResult = $tmdbApi->searchTvShow($searchQuery);
// Filter movies and TV shows by genre ID and limit results to 10
$filteredMovies = array_values(array_filter($moviesResult['results'], function($movie) {
return in_array(16, $movie['genre_ids']);
}));
$filteredTVShows = array_values(array_filter($tvShowsResult['results'], function($tvShow) {
return in_array(16, $tvShow['genre_ids']);
}));
// Extract required fields and limit results to 10
$filteredMovies = array_map(function($movie) {
return [
'id' => $movie['id'],
'title' => $movie['title'],
'image' => 'https://image.tmdb.org/t/p/w500' . $movie['poster_path']
];
}, array_slice($filteredMovies, 0, 10));
$filteredTVShows = array_map(function($tvShow) {
return [
'id' => $tvShow['id'],
'name' => $tvShow['name'],
'image' => 'https://image.tmdb.org/t/p/w500' . $tvShow['poster_path']
];
}, array_slice($filteredTVShows, 0, 10));
$searchResults = [
'movies' => $filteredMovies,
'tv_shows' => $filteredTVShows
];
// Output search results as JSON
header('Content-Type: application/json');
echo json_encode($searchResults);
exit;
}
?>