How to get Soundcloud embed code by soundcloud.com url

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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?

Edit: 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('');//the iframe is copied from soundcloud embed codes     } ); 

回答1:

For the track you selected, the embed code is such:

 

The only thing unique to it is the Track ID, in this case it's 101276036.

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) 

You can then store this ID, or generate and store the HTML that you want to be displayed.



回答2:

I found this in my codesnippets for exactly your purpose, even withouth having to register a client 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); 


回答3:

I was looking for something similar and found this really helpfull:

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

Just try this CURL command:

curl "http://soundcloud.com/oembed" -d 'format=json' -d 'url=http://soundcloud.com/forss/flickermood' 

Or this jQuery ajax request:

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); });


回答4:

This is a jQuery-based Javascript solution which I wrote.

It doesn't require a Client ID.

Make sure jQuery is included, and add this to the of your document:

To insert a Soundcloud player into a page create a

, assign it a class of sc-embed, and give it an attribute named sc_url, which should be set to the URL of the Soundcloud page.

For example:

To insert multiple players use multiple

s with different values for sc_url.

You can alter the params variable in the Javascript to enable or disable player options as listed here: https://developers.soundcloud.com/docs/oembed#introduction



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!