Fetching tracks from SoundCloud user with PHP
Introduction
I've been working on a website for an artist called Vekktone who is a very talented EDM producer whose music I found randomly on YouTube while browsing for chillstep music, after listening to a few of his tracks I really liked them and wanted to learn a little more about him, I noticed he did not have a website yet, so I offered to build him a very simple basic website, which he was very positive and enthusiastic about, I went and registered vekktone.com and started planning the website.
Creating an App
I knew I wanted to have his SoundCloud music listen on the website, so I did some "documentation skimming" over at the Soundcloud developer website and figured out that I needed to "register an app" to get a client ID that I needed to access their HTTP API.
Registering an app is pretty straightforward and you should not have any issues doing that part by yourself.
When I looked for examples in the documentation, I didn't find any PHP specific "grab all user info and spit out a list of tracks" type things, so I decided that I would make life a tiny bit easier for future developers who want to do exactly that: grab all information about a user's songs on soundcloud and display them.
Coding time!
I wrote a quick and dirty way to fetch user information with PHP, the example they had on the website about grabbing the user information was using CURL, but for the sake of simplicity I'll be using the PHP function: file_get_contents() to fetch the contents of the HTTP API endpoint(fancy word for URL) while passing the client id I got when registered my app as a parameter.
For some reason the soundcloud API does not allow you to use the username of a user, so you have to find the user id based on their username, this can be done using this little app I created while writing this post, or by using this script on your own web server:
<?php
// build our API URL
$url = "http://api.soundcloud.com/resolve.json?"
. "url=http://soundcloud.com/"
. "USERNAME-HERE"
. "&client_id=CLIENT-ID-HERE";
// Grab the contents of the URL
$user_json = file_get_contents($url);
// Decode the JSON to a PHP Object
$user = json_decode($user_json);
// Print out the User ID
echo $user->id;
?>
To display user information you need to fetch the contents of the URL: http://api.soundcloud.com/users/\*USERNAME HERE*/tracks.json?client_id=*YOUR CLIENT ID HERE* The content of that page will be a bunch of JSON which we will convert into an array of PHP objects using the json_decode() function in PHP.
If we take a look at the decoded JSON using the print_r() function wrapped in <pre>
tags for a more human readable formatting by using this code:
<?php
$clientid = "YOUR CLIENT ID HERE"; // Your API Client ID
$userid = "5925312"; // ID of the user you are fetching the information for
$soundcloud_url = "http://api.soundcloud.com/users/{$userid}/tracks.json?client_id={$clientid}";
$tracks_json = file_get_contents($soundcloud_url);
$tracks = json_decode($tracks_json);
echo "<pre>";
print_r($tracks);
echo "</pre>";
?>
We will get an output that looks like this. NOTE: I've only included the first element in the array because it would take up too much space to display them all in this post.
Array
(
[0] => stdClass Object
(
[kind] => track
[id] => 148969541
[created_at] => 2014/05/11 10:19:58
[user_id] => 5925312
[duration] => 134490
[commentable] => 1
[state] => finished
[original_content_size] => 5378159
[sharing] => public
[tag_list] => Ambient "Minimal Techno"
[permalink] => domestic-abuse-preview
[streamable] => 1
[embeddable_by] => all
[downloadable] =>
[purchase_url] =>
[label_id] =>
[purchase_title] =>
[genre] => Psytrance
[title] => Domestic Abuse (Preview)
[description] => http://www.ncdsv.org/images/LH_Update-on-the-Lisa-911-Tape_7-1-2008.pdf
[label_name] =>
[release] =>
[track_type] =>
[key_signature] =>
[isrc] =>
[video_url] =>
[bpm] =>
[release_year] =>
[release_month] =>
[release_day] =>
[original_format] => mp3
[license] => all-rights-reserved
[uri] => https://api.soundcloud.com/tracks/148969541
[user] => stdClass Object
(
[id] => 5925312
[kind] => user
[permalink] => helgesverre
[username] => HelgeSverre
[uri] => https://api.soundcloud.com/users/5925312
[permalink_url] => http://soundcloud.com/helgesverre
[avatar_url] => https://i1.sndcdn.com/avatars-000051774769-z3auai-large.jpg?debc7fd
)
[permalink_url] => http://soundcloud.com/helgesverre/domestic-abuse-preview
[artwork_url] =>
[waveform_url] => https://w1.sndcdn.com/niRFfsDpUhhR_m.png
[stream_url] => https://api.soundcloud.com/tracks/148969541/stream
[playback_count] => 40
[download_count] => 0
[favoritings_count] => 2
[comment_count] => 0
[attachments_uri] => https://api.soundcloud.com/tracks/148969541/attachments
[policy] => ALLOW
)
)
Looking at this structure we can see all the information that we can extract from the object, in our case we will just be pulling the track title, which has the name "title", for more detailed information on what all of these fields are, you should consult THIS PAGE in the API documentation, although most of them are pretty self-explanatory.
Moving on. Once we've decoded the JSON into an Object we will use the foreach() construct to iterate over it and display the information that we want our webpage to contain.
In this example we are just print out all the track names for my SoundCloud account (User: helgesverre, ID: 5925312).
So, here is how you would display a list of all the tracks that I have put on SoundCloud using PHP:
<?php
$clientid = "1cf1ebd97aeb057f0f6711daed0d7e80"; // Your API Client ID
$userid = "40706431"; // ID of the user you are fetching the information for
$soundcloud_url = "http://api.soundcloud.com/users/{$userid}/tracks.json?client_id={$clientid}";
$tracks_json = file_get_contents($soundcloud_url);
$tracks = json_decode($tracks_json);
foreach ($tracks as $track) {
echo $track->title . "
";
}
?>
And that is pretty much it.
Thanks for reading, if this helped you be sure to tweet about it share it with your dev-friends. :)