added session to prevent repeated API calls and allow data to be toggled between core and explore.
This commit is contained in:
+8
-39
@@ -3,51 +3,14 @@ require_once 'vendor/autoload.php';
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
|
||||
$dotenv->safeLoad();
|
||||
$html = '';
|
||||
|
||||
session_start();
|
||||
$client_id = $_ENV['SPOTIFY_CLIENT_ID'];
|
||||
$client_secret = $_ENV['SPOTIFY_CLIENT_SECRET'];
|
||||
$auth_code = $_GET['code'];
|
||||
|
||||
$client_credentials = get_client_credentials($client_id, $client_secret);
|
||||
|
||||
// TODO: Dynamically retrieve top 10 albums from Spotify API
|
||||
// TODO: Implement server-side caching for faster load times.
|
||||
$albums = [
|
||||
'2FQieUp8BxPN7OR8fE76TE',
|
||||
'5mUo2e4QpUA7NJl2t51uFu',
|
||||
'7ycBtnsMtyVbbwTfJwRjSP',
|
||||
'2ZytN2cY4Zjrr9ukb2rqTP',
|
||||
'4ZDBQSIDIZRUBOG2OHcN3T',
|
||||
'1FHREwXgTQvqiG8q5KlRzc',
|
||||
'1MmVkhiwTH0BkNOU3nw5d3',
|
||||
'79dL7FLiJFOO0EoehUHQBv',
|
||||
'35qVMfUfBN6q2xzm9rZn5a',
|
||||
'2eQMC9nJE3f3hCNKlYYHL1'
|
||||
];
|
||||
|
||||
shuffle($albums);
|
||||
|
||||
$content = '';
|
||||
foreach ($albums as $album_id) {
|
||||
$data = get_album($client_credentials, $album_id);
|
||||
$content = $content . '<tr>
|
||||
<td><img src="' . $data['images'][0]['url'] . '" /></td>
|
||||
<td>' . $data['name'] . '</td>
|
||||
<td>' . $data['artists'][0]['name'] . '</td>
|
||||
<td><a href="' . $data['external_urls']['spotify'] . '" target="_blank">Spotify</a></td>
|
||||
</tr>';
|
||||
}
|
||||
|
||||
$html = '
|
||||
<table>
|
||||
<tr>
|
||||
<th>Art</th>
|
||||
<th>Name</th>
|
||||
<th>Artist</th>
|
||||
<th>Spotify</th>
|
||||
</tr>' . $content . '</table>';
|
||||
|
||||
function get_client_credentials($client_id, $client_secret)
|
||||
{
|
||||
$url = 'https://accounts.spotify.com/api/token';
|
||||
@@ -173,6 +136,7 @@ function get_albums($auth_code, $client_id, $client_secret)
|
||||
curl_close($ch);
|
||||
|
||||
$song_data = [];
|
||||
if ($_SESSION['data'] == null) {
|
||||
foreach ($top_songs as $song) {
|
||||
$song_data[] = array(
|
||||
'image' => $song['album']['images'][0]['url'],
|
||||
@@ -184,6 +148,12 @@ function get_albums($auth_code, $client_id, $client_secret)
|
||||
);
|
||||
}
|
||||
|
||||
echo "saving data to $_SESSION";
|
||||
$_SESSION['data'] = $song_data;
|
||||
} else {
|
||||
$song_data = $_SESSION['data'];
|
||||
}
|
||||
|
||||
foreach ($song_data as $s) {
|
||||
echo '
|
||||
<img src="' . $s['image'] . '">
|
||||
@@ -196,5 +166,4 @@ function get_albums($auth_code, $client_id, $client_secret)
|
||||
}
|
||||
|
||||
get_albums($auth_code, $client_id, $client_secret);
|
||||
// echo $html;
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user