Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get track ID from URL using the SoundCloud API

How can I get a track ID from a track URL using the SoundCloud API?

like image 303
floCoder Avatar asked Oct 10 '14 00:10

floCoder


People also ask

How do you get the URL from SoundCloud?

Step 1 | Retrieving a SoundCloud URL or embed code:In SoundCloud, search for the music you want. Click Share on the music. Copy the URL.

Can I use SoundCloud API?

To access the SoundCloud® API, you will first need to register your app at https://soundcloud.com/you/apps using your SoundCloud® account. When you've done that, we'll issue you with a client ID and client secret. Your client ID is required for all calls to the SoundCloud® API.

What can you do with SoundCloud API?

Our API gives you the ability to upload, manage and share tracks. Your app can take an audio file and upload it to a user's SoundCloud account. You can also manage metadata (including tags) or add the track to playlists.


2 Answers

You didn't specify a language in your question, so I'm just going to provide a high-level answer.

Use the resolve endpoint. I'm using this URL as an example:

https://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix

HTTP GET: https://api.soundcloud.com/resolve.json?url=https%3A%2F%2Fsoundcloud.com%2Fmsmrsounds%2Fms-mr-hurricane-chvrches-remix&client_id=[your_client_id]

The resulting JSON:

{
  "status": "302 - Found",
  "location": "https://api.soundcloud.com/tracks/147986343.json?client_id=[YOUR client_id]"
}

That numerical value is the track_id. You can just stop here or hit that endpoint you just got back for more information:

{
  "kind": "track",
  "id": 90787841,
  "created_at": "2013/05/05 00:15:40 +0000",
  "user_id": 7853935,
  "duration": 188333,
  "commentable": true,
  "state": "finished",
  "original_content_size": 35031144,
  "last_modified": "2014/09/30 05:53:10 +0000",
  "sharing": "public",
  "tag_list": "MSMR CHVRCHES \"Secondhand Rapture\" Remix Hurricane",
  "permalink": "ms-mr-hurricane-chvrches-remix",
  "streamable": true,
  "embeddable_by": "all",
  "downloadable": true,
  "purchase_url": null,
  "label_id": null,
  "purchase_title": null,
  "genre": "",
  "title": "MS MR - Hurricane (CHVRCHES remix)",
  "description": "MS MR - Hurricane (CHVRCHES remix)\r\n\r\n\r\n",
  "label_name": "",
  "release": "",
  "track_type": "",
  "key_signature": "",
  "isrc": "",
  "video_url": null,
  "bpm": null,
  "release_year": null,
  "release_month": null,
  "release_day": null,
  "original_format": "wav",
  "license": "all-rights-reserved",
  "uri": "https://api.soundcloud.com/tracks/90787841",
  "user": {
    "id": 7853935,
    "kind": "user",
    "permalink": "msmrsounds",
    "username": "MSMR",
    "last_modified": "2014/08/04 09:03:56 +0000",
    "uri": "https://api.soundcloud.com/users/7853935",
    "permalink_url": "http://soundcloud.com/msmrsounds",
    "avatar_url": "https://i1.sndcdn.com/avatars-000037198343-2lptmy-large.jpg?86347b7"
  },
  "permalink_url": "http://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix",
  "artwork_url": null,
  "waveform_url": "https://w1.sndcdn.com/56LCadzDEBZn_m.png",
  "stream_url": "https://api.soundcloud.com/tracks/90787841/stream",
  "download_url": "https://api.soundcloud.com/tracks/90787841/download",
  "playback_count": 1672227,
  "download_count": 18494,
  "favoritings_count": 20426,
  "comment_count": 464,
  "attachments_uri": "https://api.soundcloud.com/tracks/90787841/attachments",
  "policy": "ALLOW"
}
like image 190
JAL Avatar answered Sep 19 '22 12:09

JAL


So my final code looks like this. Thank you guys for the help to get this working.

include_once('../Services/Soundcloud.php'); 
$client = new Services_Soundcloud('CLIENT_ID', 'CLIENT_SECRET');
$track_url=$_POST['trackurl'];
$track_url=str_replace("https://soundcloud.com/USERNAME/","",$track_url);
$track = json_decode($client->get("tracks/".$track_url));    
$track_id=$track->id;
echo $track_id;
like image 44
floCoder Avatar answered Sep 18 '22 12:09

floCoder