【问题标题】:Fetching cover art via the Spotify API通过 Spotify API 获取封面艺术
【发布时间】:2014-06-18 08:46:18
【问题描述】:

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

访问以下网址:

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

【问题讨论】:

    标签: php json api spotify


    【解决方案1】:

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

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

    See answers related to that error here.

    因此,无需摆弄 PHP 的最简单的解决方案是使用 cURL 代替 CURLOPT_SSL_VERIFYHOSTCURLOPT_SSL_VERIFYPEER 属性设置为 false

    所以代码是: 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] => 
    )
    */
    

    【讨论】:

    • 非常感谢,洛兹!这对我帮助很大。我现在对这些事情有了更好的理解 :-) 再次感谢!
    【解决方案2】:

    看起来您需要设置一个用户代理,而不是 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 可以更精细地处理您的请求。在这个例子中,我们设置了一个用户代理。

    【讨论】:

    • 感谢您的提示。奇迹般有效! :) 真的很感激!
    【解决方案3】:

    这就是我在 Python 中的做法,这对我来说效果很好。我看到的主要区别是网址中的&amp;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
    

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多