Dan
Dan

Reputation: 57

Soundcloud get track ID from link (PHP)

Okay, so I'm using php to post soundcloud widgets on my website(no need to explain why and how because thath's not wath's important)

Here is my code:

if (strpos($post['link'], 'soundcloud.com') !== FALSE)
{
    echo "<iframe width=\"500\" height=\"150\" scrolling=\"no\" frameborder=\"no\" 
    src=\"https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/".$trackID."&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true\"></iframe>";
}

My variable $post['link'] outputs the soundcloud link in this form https://soundcloud.com/alex-oancea/best-thing-about-you-original

What I would like to do is get the track's id from the link and declare it as $trackID because as you can see in my code(in the src="" part), the widget uses the track's ID

I have looked into the soundcloud API and many other threads on this site but I didin't find how to do this or how to write the code...

Thanks in advance!

Upvotes: 0

Views: 8342

Answers (3)

Devour
Devour

Reputation: 51

Assuming you are using the php-soundcloud from the mptre's Github:

Check https://developers.soundcloud.com/docs#resolving (and even the '/resolve' indicated in the 'see also' section).

You can get a good way of getting a track ID by this way:

<?php
require_once 'Services/Soundcloud.php';

// create a client object with your app credentials
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');

// a permalink to a track
$track_url = 'http://soundcloud.com/forss/voca-nomen-tuum';

// resolve track URL into track resource
$track = json_decode($client->get('resolve', array('url' => $track_url),        array(CURLOPT_FOLLOWLOCATION => true))); 

// display all the information from that track resource
var_dump($track);
Now $track->id is your track ID.
?>

Just be aware you need the SoundCloud API for this, and you have to register your app with SoundCloud to use the service (Register your app).

Another information is that Webservers where a basedir restriction is set and/or safe mode is active don't allow the CURLOPT_FOLLOWLOCATION = true option. Here is a workaround for these webservers in the Soundcloud.php file.

Upvotes: 0

Mihai Miculescu
Mihai Miculescu

Reputation: 3

If you experiment on your local machine (xampp in your case), you CANNOT make it work, beause the 'REDIRECT_URL' cannot be anything else but a valid link to a hosted domain. The API just does not accept (for the good reason) url's containing "localhost" or or "192.168.x.x"-style url's. This is simply because the API really sends info to the 'REDIRECT_URL' and it needs to be able to reach it. You need to get in touch with your hosting provider and setup a "live" dev environement.

Upvotes: 0

Scopey
Scopey

Reputation: 6319

Check http://developers.soundcloud.com/docs#resolving

You can click the PHP button on the example and get a good way of getting a track ID:

<?php
require_once 'Services/Soundcloud.php';

// create a client object with your app credentials
$client = new Services_Soundcloud('YOUR_CLIENT_ID');

// a permalink to a track
$track_url = 'http://soundcloud.com/forss/voca-nomen-tuum';

// resolve track URL into track resource
$track = json_decode($client->get('resolve', array('url' => $track_url)));

Now $track->id is your track ID.

Just be aware you need the SoundCloud API for this, and you'll probably have to register your app with SoundCloud to use the service. I have not had personal experience with any of this so I can't provide any information about those processes.

Upvotes: 1

Related Questions