Skip to content

Instantly share code, notes, and snippets.

@dexit
Forked from KimJoyFox/spotifyAPI.php
Created April 22, 2024 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexit/8fc9b9198ac361b3eaa3d4563f592309 to your computer and use it in GitHub Desktop.
Save dexit/8fc9b9198ac361b3eaa3d4563f592309 to your computer and use it in GitHub Desktop.
Display Spotify Episodes on Your Website: Simple API
<?php
add_shortcode( 'spotify', 'getSpotify' );
function getSpotify(){
// Add 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' with your actual Spotify API credentials
$client_id = '';
$client_secret = '';
$show_id = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/api/token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Basic ' . base64_encode($client_id . ':' . $client_secret),
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
$access_token = $data['access_token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.spotify.com/v1/shows/' . $show_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $access_token,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);
$results = json_decode($response, true);
ob_start();
?>
<div class="podcast_row">
<?php
$total_episodes = $results['total_episodes'];
if ( is_array($results['episodes']) ){
if ( array_key_exists('items', $results['episodes']) ){
$eachPod = $results['episodes']['items'];
$i = 0;
foreach ( $eachPod as $pod ){
if ($i <= 3){
$name = $pod['name'];
$URL = $pod['external_urls']['spotify'];
$ID = $pod['id'];
?>
<div class="column">
<a href="<?php echo $URL; ?>" target="_blank">
<p>Episode #<?php echo $total_episodes - $i; ?></p>
<p class="title"><?php echo $name; ?></p>
</a>
</div>
<?php
}
$i++;
}
}
} ?>
</div><?php
$content = ob_get_clean();
return $content;
//echo '<pre>';
//print_R($results);
//echo '</pre>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment