如何通过soundcloud.com网址获取Soundcloud嵌入代码

And*_*Liu 17 javascript php soundcloud

我一直在研究几个小时没有运气才能让它奏效.基本上我有一个cms,用户可以在其中放置soundcloud url,如" https://soundcloud.com/cade-turner/cade-turner-symphony-of-light ",然后该页面可以显示嵌入式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 +'&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>');//the iframe is copied from soundcloud embed codes
    }
);
Run Code Online (Sandbox Code Playgroud)

小智 27

我在我的代码片段中找到了这个用于您的目的,即使不必注册客户端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);
Run Code Online (Sandbox Code Playgroud)

  • 这在此处记录https://developers.soundcloud.com/docs/oembed#introduction (3认同)

Jac*_*din 11

对于您选择的曲目,嵌入代码如下:

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/101276036&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>
Run Code Online (Sandbox Code Playgroud)

唯一独有的是Track 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)
Run Code Online (Sandbox Code Playgroud)

然后,您可以存储此ID,或生成并存储要显示的HTML.

  • @Richard Soundcloud 神秘地清理了他们的 PHP SDK 文档。但基于 [这篇博文](https://blog.soundcloud.com/2009/09/18/php-wrapper/),[HTTP API 指南代码片段](https://developers.soundcloud.com/ docs/api/guide) 可能基于 [mptre/php-soundcloud](https://github.com/mptre/php-soundcloud) 项目。 (2认同)

rob*_*ckx 9

我正在寻找类似的东西,发现这真的很有帮助:

https://developers.soundcloud.com/docs/oembed#introduction

试试这个CURL命令:

curl "http://soundcloud.com/oembed" -d 'format=json' -d 'url=http://soundcloud.com/forss/flickermood'
Run Code Online (Sandbox Code Playgroud)

或者这个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);
});
Run Code Online (Sandbox Code Playgroud)