-
-
Save dexit/8fc9b9198ac361b3eaa3d4563f592309 to your computer and use it in GitHub Desktop.
Display Spotify Episodes on Your Website: Simple API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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