How to get Soundcloud embed code by soundcloud.com url

Viewed 32248

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('<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
    }
);
8 Answers

I ran into the same problem as above. I have old embed code that no longer works, and unfortunately for me I'm limited to HTML only. Seeing the answers above this just doesn't work for me. So I decided, I'll just try something and see if it works and oh my, it actually works! No longer do you have to actually convert stuff. You can just use the old url. Here's the html code for me:

<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>

I've added enters above to make it readable, but should be one line of code.

The reason is that I run this on a forum with a custom BBCode mode, where I want to support it as follows: [soundcloud=Artist]song[/soundcloud]

In the above case, this is [soundcloud=LPChip]Internal-meganism[/soundcloud].

The actual html for the BBCode is as follows:

<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>

This works for SMF 2.0.15 with custom BBCode mod, set as unparsed equal content.

demo: https://nifflas.lp1.nl/index.php?topic=6630.0

I'm not sure if anyone tried this before, but currently (Feb 2021) It's working to just pass the basic soundcloud link into the iframe link's ?url parameter. The "official" embed code uses another ID, but the regular link works fine too (for now).

For example, this link to soundcloud can be embedded as 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>

Props to @lpchip for his answer that inspired me to try this

I've done it this way, in javascript, without using 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);
});

Here is an example in PHP with CURL

// 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);
Related