What are the differences between displaying the current track on a homepage and creating a history of played tracks in a database using PHP for a Shoutcast server?
Displaying the current track on a homepage involves retrieving the current track information from the Shoutcast server and displaying it on the webpage in real-time. On the other hand, creating a history of played tracks in a database using PHP requires storing the track information in a database whenever a new track is played, allowing users to view a list of previously played tracks.
// Displaying the current track on a homepage
<?php
$server_ip = 'your_shoutcast_server_ip';
$server_port = 'your_shoutcast_server_port';
$data = file_get_contents("http://$server_ip:$server_port/currentsong?sid=1");
$track_info = json_decode($data, true);
echo "Current Track: " . $track_info['title'] . " by " . $track_info['artist'];
?>
// Creating a history of played tracks in a database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "shoutcast_history";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$server_ip = 'your_shoutcast_server_ip';
$server_port = 'your_shoutcast_server_port';
$data = file_get_contents("http://$server_ip:$server_port/currentsong?sid=1");
$track_info = json_decode($data, true);
$sql = "INSERT INTO played_tracks (title, artist) VALUES ('" . $track_info['title'] . "', '" . $track_info['artist'] . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>