Question

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;
?>
Was it helpful?

Solution 2

Looks like you need to set a User Agent, not 100% as I haven't checked the docs, but setting a User Agent with CURL allows me to receive the data. Try the following:

<?php
$track = "spotify:track:5ChkMS8OtdzJeqyybCc9R5";
$url = "https://embed.spotify.com/oembed/?url=".$track."&format=json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x");
$output = curl_exec($ch);
curl_close($ch);

$get_json  = json_decode($output);
$cover     = $get_json->thumbnail_url;
echo $cover;

Using CURL allows you to be more granular with your request. In this example we are setting a User Agent.

OTHER TIPS

The reason file_get_contents wont work is if you enable error reporting you will see the error:

Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

See answers related to that error here.

So the easiest solution without fiddling with PHP is to use cURL instead with the CURLOPT_SSL_VERIFYHOST & CURLOPT_SSL_VERIFYPEER properties set to false.

So the code is: See in action!

<?php
$track = "spotify:track:5ChkMS8OtdzJeqyybCc9R5";
$url = "https://embed.spotify.com/oembed/?url=".$track."&format=json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; curl)");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$json = curl_exec($ch);
curl_close($ch);

$json  = json_decode($json);

$cover = $json->thumbnail_url;

//https://d3rt1990lpmkn.cloudfront.net/cover/e337f3661f68bc4d96a554de0ad7988d65edb25a
echo $cover;

//Debug - Complete response
echo '<pre>'.print_r($json, true).'</pre>';
/*
stdClass Object
(
    [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/d45aa25bbd45872c0b1e97223af57fe94588820a
    [provider_name] => Spotify
    [type] => rich
    [html] => 
)
*/

This is how I'm doing it in Python, and this has been working well for me. The main difference I see is the &callback= in the url, which seemed to be required at the time I wrote this.

import requests
def getArt(spTrackURL,x=False):
    """
        Takes a uri to a spotify track
        -> Use uri to query Spotify web service for album art path
        -> modify art path to produce 300 px image (larger than default, no logo) 
        -> return art path as string
    """
    if (not x):
        log("getArt","for '" + spTrackURL + "' -> Getting cover art from Spotify")
    spEmbedUrl = 'https://embed.spotify.com/oembed/?url=' + spTrackURL + '&callback=?'
    try:
        r = requests.get(spEmbedUrl)
        while (r.text == ''):
            time.sleep(1)
        t = r.text.split(',')
        for i in t:
            if (i.find('thumbnail_url') != -1):
                t = i
        t = t.replace('"thumbnail_url":"','').replace('"', '').replace('\\','').replace('cover','300')
        #print t
    except:
        t = ''
        #print 'something bad happened when getting art, trying again'
        t = getArt(spTrackURL, True)
    return t
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top