I would like a user of my app to
- paste in a Soundcloud track URL to a
<input>
field
- press a submit
<button>
- have a function return the track's
id
So far the only way I know how to do a /resolve
is via command line's curl
command as demonstrated here http://developers.soundcloud.com/docs/api/reference#resolve. Is there a way to do this via a Javascript function?
e.g.
SC.resolve(trackURL, id, function(id, error){});
All curl
does is an HTTP request. JavaScript is definitely able to do an HTTP request, most common way of doing that is by using XMLHttpRequest also known as AJAX.
Popular libraries, such as jQuery simplify issuing these requests, for example:
// replace YOUR_CLIENT_ID with one you can get from http://soundcloud.com/you/apps
var trackUrl = 'http://soundcloud.com/matas/hobnotropic';
$.get(
'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=YOUR_CLIENT_ID',
function (result) {
console.log(result);
}
);
Working example here.
You can actually use SC.get() directly for almost every resources in the API (without jQuery). Like this:
<script src="http://connect.soundcloud.com/sdk.js"></script>
<script>
SC.initialize({
client_id: "YOUR_CLIENT_ID"
});
SC.get("/resolve/?url=https://soundcloud.com/dogwill", {limit: 1}, function(result){
console.log(result);
});
</script>