问题描述
我一直在研究几个小时没有运气才能让它奏效。基本上我有一个cms,用户可以在其中放置soundcloud url,然后页面可以显示嵌入的iframe。我已经将api文档发送了一段时间,但找不到任何相关内容。这篇文章有谈了但我只是不太明白答案,我检查了和但找不到合适的例子。任何人都有任何提示吗?
I have been researching for hours with no luck to get it worked. Basically I have a cms in which users can put soundcloud url like this "https://soundcloud.com/cade-turner/cade-turner-symphony-of-light", then the page can show an embedded iframe. I have red the api docs for a while but couldn't find anything relavant. This post here have talked but i just didn't quite understand the answers and I checked oEmbed and oEmbed reference but couldn't find proper example. Any one has any more hints?
编辑:
感谢雅各布的回答,我终于设法通过使用ajax来实现。
Thanks to Jacob's answer, I finally managed to do it by using ajax.
var trackUrl = 'THE_URL';
var Client_ID = 'CLIENT_ID';//you have to register in soundcound developer first in order to get this id
$.get(//make an ajax request
'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + Client_ID,
function (result) {//returns json, we only need id in this case
$(".videowrapper, .exhibitions-image, iframe").replaceWith('<iframe width="100%" height="100%" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + result.id +'&color=ff6600&auto_play=false&show_artwork=true"></iframe>');//the iframe is copied from soundcloud embed codes
}
);
推荐答案
对于您选择的曲目,嵌入代码是这样的:
For the track you selected, the embed code is such:
<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/101276036&color=ff6600&auto_play=false&show_artwork=true"></iframe>
唯一独有的是Track ID,在这种情况下它是 101276036
。
The only thing unique to it is the Track ID, in this case it's 101276036
.
因此,当您只有URL时,您的真正问题是尝试查找Track ID,并且Soundcloud API提供了一个名为那。
So your real issue is trying to find the Track ID when you only have the URL, and the Soundcloud API provides a method called resolve
to do just that.
require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud('YOUR_CLIENT_ID');
$track_url = 'https://soundcloud.com/cade-turner/cade-turner-symphony-of-light'; // Track URL
$track = json_decode($client->get('resolve', array('url' => $track_url)));
$track->id; // 101276036 (the Track ID)
然后,您可以存储此ID,或生成并存储HTML你想要显示。
You can then store this ID, or generate and store the HTML that you want to be displayed.
这篇关于如何通过soundcloud.com网址获取Soundcloud嵌入代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!