I'm trying to work a bit with the Spotify API to get the cover art URL pulled from their response. In this example, I'm trying to get the cover art from Michael Jackson’s “Billie Jean”. It seems really straight forward, but I’m no expert at all - I'm just playing around to see if I can work this out.
Visit the following URL:
https://embed.spotify.com/oembed/?url=spotify:track:5ChkMS8OtdzJeqyybCc9R5&format=json&callback=spotify
Returns the following JSON response:
spotify({"provider_url":"https:\/\/www.spotify.com","version":"1.0","thumbnail_width":300,"height":380,"thumbnail_height":300,"title":"Michael Jackson - Billie Jean - Single Version","width":300,"thumbnail_url":"https:\/\/d3rt1990lpmkn.cloudfront.net\/cover\/e337f3661f68bc4d96a554de0ad7988d65edb25a","provider_name":"Spotify","type":"rich","html":"<iframe src=\"https:\/\/embed.spotify.com\/?uri=spotify:track:5ChkMS8OtdzJeqyybCc9R5\" width=\"300\" height=\"380\" frameborder=\"0\" allowtransparency=\"true\"><\/iframe>"});
What I’m trying to do is to get the PHP script to pull the thumbnail_url
and echo it in the document. However, I only get error messages. Can anyone help me out, and point out what I'm doing wrong?
This is my script so far:
<?php
$track = "spotify:track:5ChkMS8OtdzJeqyybCc9R5";
$url = "https://embed.spotify.com/oembed/?url=".$track."&format=json&callback=spotify";
$get_data = file_get_contents($url);
$get_json = json_decode($get_data);
$cover = $get_json->spotify->thumbnail_url;
echo $cover;
?>