通过Spotify API获取封面艺术


Fetching cover art via the Spotify API

我正在尝试使用Spotify API从他们的响应中获取封面URL。在这个例子中,我试图从迈克尔·杰克逊的"Billie Jean"中获取封面艺术。这似乎很简单,但我根本不是专家,我只是试着看看我能不能解决这个问题。

访问以下URL:

https://embed.spotify.com/oembed/?url=spotify:track:5ChkMS8OtdzJeqyybCc9R5&format=json&callback=spotify

返回以下JSON响应:

  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>"});

我要做的是让PHP脚本拉thumbnail_url并在文档中回显它。但是,我只得到错误消息。谁能帮帮我,指出我做错了什么吗?

这是目前为止我的脚本:

<?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;
?>

file_get_contents不起作用的原因是如果您启用了错误报告,您将看到错误:

警告:file_get_contents() [function。file-get-contents]:无法找到包装器"https"—您是否忘记启用它配置PHP吗?

查看与此错误相关的答案

所以最简单的解决方案是使用cURL代替CURLOPT_SSL_VERIFYHOST &CURLOPT_SSL_VERIFYPEER属性设置为false .

所以代码是: 看在行动!

<?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] => 
)
*/

看起来您需要设置一个用户代理,而不是100%,因为我还没有检查文档,但是使用CURL设置一个用户代理允许我接收数据。试试以下命令:

<?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;

使用CURL可以让您对请求进行更细粒度的处理。在这个例子中,我们正在设置一个用户代理。

这就是我在Python中做的,这对我来说工作得很好。我看到的主要区别是url中的&callback=,这在我写这篇文章的时候似乎是必需的。

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