我已经研究了几个小时,但没有运气让它工作。基本上我有一个cms,用户可以在其中放置像"https://soundcloud.com/cade-turner/cade-turner-symphony-of-light"这样的soundcloud url,然后页面可以显示嵌入式iframe。我已经红色的 api 文档一段时间了,但找不到任何内容。这篇文章在这里已经讨论过,但我只是不太明白答案,我检查了 oEmbed 和 oEmbed 参考,但找不到合适的例子。还有什么提示吗?
编辑:多亏了 Jacob 的回答,我终于设法通过使用 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
}
);
我在我的代码片段中找到了它,完全是为了您的目的,即使不必注册客户端 ID。
//Get the SoundCloud URL
$url="https://soundcloud.com/epitaph-records/this-wild-life-history";
//Get the JSON data of song details with embed code from SoundCloud oEmbed
$getValues=file_get_contents('http://soundcloud.com/oembed?format=js&url='.$url.'&iframe=true');
//Clean the Json to decode
$decodeiFrame=substr($getValues, 1, -2);
//json decode to convert it as an array
$jsonObj = json_decode($decodeiFrame);
//Change the height of the embed player if you want else uncomment below line
// echo $jsonObj->html;
//Print the embed player to the page
echo str_replace('height="400"', 'height="140"', $jsonObj->html);
对于您选择的曲目,嵌入代码如下所示:
<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>
它唯一独特的是 跟踪ID,在这种情况下是 101276036
.
因此,您真正的问题是在只有URL时尝试查找Track ID,而Soundcloud API提供了一种称为resolve
的方法来实现这一点。
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。
我正在寻找类似的东西,发现这真的很有帮助:
https://developers.soundcloud.com/docs/oembed#introduction
只需尝试此 CURL 命令:
curl "http://soundcloud.com/oembed" -d 'format=json' -d 'url=http://soundcloud.com/forss/flickermood'
或者这个jQuery ajax请求:
var settings = {
"async": true,
"crossDomain": true,
"url": "http://soundcloud.com/oembed",
"method": "POST",
"headers": {},
"data": {
"format": "json",
"url": "http://soundcloud.com/forss/flickermood"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我遇到了与上面相同的问题。我有旧的嵌入代码不再有效,不幸的是,对我来说,我仅限于 HTML。看到上面的答案对我不起作用。所以我决定,我会尝试一些东西,看看它是否有效,哦,天哪,它真的有效!您不再需要实际转换内容。您可以只使用旧网址。这是我的 html 代码:
<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay"
src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/LPChip/internal-meganism
&color=%23ff5500&auto_play=false&hide_related=false
&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true">
</iframe>
我在上面添加了 Enter 以使其可读,但应该是一行代码。
原因是我在具有自定义BBCode模式的论坛上运行它,我想在其中支持它,如下所示:[soundcloud=Artist]song[/soundcloud]
在上述情况下,这是[soundcloud=LPChip]Internal-meganism[/soundcloud]
.
BBCode的实际html如下:
<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/{option1}/{content}&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>
这适用于具有custom BBCode mod
的SMF 2.0.15
,设置为未解析的相等内容。
演示:https://nifflas.lp1.nl/index.php?topic=6630.0
编写的基于jQuery的Javascript解决方案。
它不需要客户端 ID。
确保包含 jQuery,并将其添加到文档的<head>
:
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
(function ($) {
$.fn.scembed = function(){
var datasource = 'http://soundcloud.com/oembed';
return this.each(function () {
var container = $(this);
var mediasource = $(container).attr("sc_url");
var params = 'url=' + mediasource + '&format=json&iframe=true&maxwidth=480&maxheight=120&auto_play=false&show_comments=false';
$.ajaxopts = $.extend($.ajaxopts, {
url: datasource,
data: params,
dataType: 'json',
success: function (data, status, raw) {
$(container).html(data.html);
},
error: function (data, e1, e2) {
$(container).html("Can't retrieve player for " + mediasource);
},
});
$.ajax($.ajaxopts);
});
};
})(jQuery);
$(function(){
$("div.sc-embed").scembed();
});
});
</script>
要将 Soundcloud 播放器插入页面,请创建一个<div>
,为其分配一类sc-embed
,并为其提供一个名为 sc_url
的属性,该属性应设置为Soundcloud页面的URL。
例如:
<div class="sc-embed" sc_url="http://soundcloud.com/forss/flickermood"></div>
要插入多个播放器,请使用具有不同值的多个<div>
sc_url
。
您可以更改 Javascript 中的 params
变量以启用或禁用播放器选项,如下所示:https://developers.soundcloud.com/docs/oembed#introduction
我不确定以前是否有人尝试过这个,但目前(2021 年 2 月)它的工作原理是将基本的声音云链接传递到 iframe 链接的 ?url 参数中。 "官方"嵌入代码使用另一个ID,但常规链接也可以正常工作(目前)。
例如,这个指向声音云的链接可以嵌入为 https://soundcloud.com/anton-jarvis-206182017/loves-philosophy-by-percy-bysshe-shelley
<iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/anton-jarvis-206182017/loves-philosophy-by-percy-bysshe-shelley&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>
道具@lpchip他的答案激发了我尝试这个
我在javascript中这样做了,没有使用jQuery:
var formData = new FormData();
formData.append("format", "json");
formData.append("url", "http://soundcloud.com/forss/flickermood");
fetch('http://soundcloud.com/oembed', {
method: 'POST',
body: formData
}).then(function (response) {
console.log(response);
});
这是使用 CURL 的 PHP 中的一个例子
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://soundcloud.com/oembed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "format=json&url=http://soundcloud.com/forss/flickermood");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);