-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.php
More file actions
64 lines (55 loc) · 1.95 KB
/
type.php
File metadata and controls
64 lines (55 loc) · 1.95 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
// Database connection settings
$servername = "";
$username = "";
$password = "";
$dbname = ""; //Your Dbname
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$audience_type = isset($_GET['audience_type']) ? $_GET['audience_type'] : '';
$offset = isset($_GET['offset']) ? intval($_GET['offset']) : 0;
$limit = 18; // Number of podcasts per batch
// Prepared statement to fetch distinct apple_ids with limit and offset
$sql = "SELECT DISTINCT apple_id FROM your_table WHERE (audience_type = ?) LIMIT ? OFFSET ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sii", $audience_type, $limit, $offset);
$stmt->execute();
$result = $stmt->get_result();
$apple_ids = [];
while ($row = $result->fetch_assoc()) {
$apple_ids[] = $row['apple_id'];
}
// Function to fetch podcast data from iTunes API
function fetchPodcastData($apple_id) {
$itunes_url = "https://itunes.apple.com/lookup?id=" . $apple_id . "&entity=podcast";
$json_data = file_get_contents($itunes_url);
return json_decode($json_data, true);
}
$podcasts = [];
foreach ($apple_ids as $apple_id) {
$podcast_data = fetchPodcastData($apple_id);
if (isset($podcast_data['results']) && count($podcast_data['results']) > 0) {
$podcasts[] = $podcast_data['results'][0];
}
}
// Get total number of distinct apple_ids for this audience type
$count_sql = "SELECT COUNT(DISTINCT apple_id) as total FROM your_table WHERE (audience_type = ?)";
$count_stmt = $conn->prepare($count_sql);
$count_stmt->bind_param("s", $audience_type);
$count_stmt->execute();
$count_result = $count_stmt->get_result();
$total_count = 0;
if ($count_result->num_rows > 0) {
$row = $count_result->fetch_assoc();
$total_count = intval($row['total']);
}
// Return podcasts and total count as JSON
echo json_encode([
'podcasts' => $podcasts,
'total' => $total_count
]);
$conn->close();
?>