Question

I'm trying to setup a simple app that should:

  • get the client ID of an artist from its URL
  • display the two latest tracks

However I'm not that practical with soundcloud and i just know basic php. I started to play with soundcloud but i wasn't able to handle it. A problem i have is that any code i write, it gets

Fatal error: Uncaught exception 'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 302.'

The easiest setup come straight from the documentation and is an example to retrieve the comments from the track id, starting from a give URL.

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

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

// a permalink to a track
$track_url = 'https://url_to_a_track';

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

// now that we have the track id, we can get a list of comments, for example
foreach (json_decode($client->get('tracks/' . $track->id . 'comments')) as $c)
print 'Someone said: ' . $c->body . ' at ' . $c->timestamp . "\n"; ?>

Just added ('CURLOPT_FOLLOWLOCATION', TRUE) because I've read about it around the web... And I always get the fatal error... why?

Was it helpful?

Solution 3

I've been able to solve it. If the resources are public, then you don't need to authenticate. Here's the code that:

  • Get user URL from a wordpress custom-field
  • Retrieve ID from json object
  • Retrieve the latest 2 tracks and display 2 embedded players

It just need your own Client ID you can get easily by registering on soundcloud developers section, and then substitute it to {Your_ID}

    // get user URL from Wordpress custom field
    $sc_url = get_post_meta(get_the_id(), 'sc_url', true);

    // if $sc_url is not empty, do
    if (!empty($sc_url)) {

    $unparsed_json = file_get_contents('https://api.soundcloud.com/resolve.json?url='.$sc_url.'&client_id={Your_ID}');

    $json_object = json_decode($unparsed_json);

    // retrieve the user ID from json_object
    $roster_id = $json_object->{'id'};

    // get last two tracks from the user and generate embed code for each tracks 
    $tracks_json = file_get_contents('http://api.soundcloud.com/users/'.$roster_id.'/tracks?client_id={Your_ID}&order=latest&limit=2&format=json');
    $tracks = json_decode($tracks_json);
    foreach ($tracks as $track){
    $trackID = $track->id;
    echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F'.$trackID.'"></iframe>';
    }}

Hope it can help others :)

OTHER TIPS

'How can I use the soundcloud API resolve resource with PHP?'

  • to use the resolve resource of the soundcloud API with php, following frafor's code, do:

    $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1)); $response = json_decode($client->get('resolve',array('url' => $track_url))); ?>

So first CURLOPT_FOLLOWLOCATION and then the API call, your where almost there! :) Hope it helps someone!

Cheers, T

the soundcloud servers have switched to secure and they now use https protocol for API / JSON as well.

My apps were not working any more so these are the other cURL options to get the JSON. SSL verification must both be set to disable. I got it working only with this.

// Configuring curl options
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json'),
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
);

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top