我正在努力使用Spotify API来从他们的回复中获取封面艺术网址。在这个例子中,我试图从Michael Jackson的“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;
?>
答案 0 :(得分:2)
file_get_contents不起作用的原因是,如果启用错误报告,您将看到错误:
警告:file_get_contents()[function.file-get-contents]:无法执行 找到包装&#34; https&#34; - 你忘了启用它吗? 配置PHP?
的 See answers related to that error here. 强>
所以最简单的解决方案就是使用cURL代替和 CURLOPT_SSL_VERIFYHOST
&amp; CURLOPT_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] =>
)
*/
答案 1 :(得分:1)
您似乎需要设置用户代理,而不是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可以更精细地处理您的请求。在此示例中,我们正在设置用户代理。
答案 2 :(得分:0)
这就是我在Python中的表现,这对我来说效果很好。我看到的主要区别是网址中的&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