Skip to content

Instantly share code, notes, and snippets.

@tlimpanont
Last active August 29, 2015 14:04
Show Gist options
  • Save tlimpanont/022c1ed8a927a7f05607 to your computer and use it in GitHub Desktop.
Save tlimpanont/022c1ed8a927a7f05607 to your computer and use it in GitHub Desktop.
Soundcloud embed page, search and combining clientId and trackId to resource URL

TO GET TO MP3 file with JSON API of soundcloud

https://api.soundcloud.com/i1/tracks/160637550/streams?client_id=0f8fdbbaa21a9bd18210986a7dc2d72c&format=json

What we need to know is the trackID and the ClientID

TrackID can be found in the data-src="" attr

<iframe src="about:blank" frameborder="no" scrolling="no" width="100%" height="166" data-src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/160637550&amp;color=ff0000&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false" class="npo_cc_social npo_cc_processed" cookiewall-checked="true"></iframe>
  1. Test whether the page contains a soundcloud Iframe
  2. Get the trackID from the data-source attribute of the Iframe tag, e.g. data-src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/160637550&color=ff0000&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"
  3. Curl html data-src url page and hijack ajax request.
 <?php
 header('Access-Control-Allow-Origin: *');
    //Get the url
    $url = $_GET['url'];

    //Get the html of url
    function get_data($url) 
    { 
       $ch = curl_init();
       $timeout = 5;
       //$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US)AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.X.Y.Z Safari/525.13.";
       $userAgent = "IE 7 – Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30)";
      curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
      curl_setopt($ch, CURLOPT_FAILONERROR, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_AUTOREFERER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch,CURLOPT_URL,$url);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
    }

    $html = file_get_contents($url);
    
?>

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
var parseQueryString = function( queryString ) {
  var params = {}, queries, temp, i, l;

  // Split into key/value pairs
  queries = queryString.split("&");

  // Convert the array of strings into an object
  for ( i = 0, l = queries.length; i < l; i++ ) {
      temp = queries[i].split('=');
      params[temp[0]] = temp[1];
  }
  return params;
};
// Hijack AJAX request 
(function(open) {
  XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
    var params = parseQueryString(url);
     window.location = "soundcloud_client.php?id=" + params.client_id;
     return false;
    open.call(this, method, rewrittenUrl, async, user, pass);
  };
})(XMLHttpRequest.prototype.open);
</script>

<?php 
// render HTML content 
echo $html;
?>
  1. Parse the query string params from XMLHttpRequest url and get the ClientID.
  2. Combine ClientID and TrackID to this URL
https://api.soundcloud.com/i1/tracks/:trackId/streams?client_id={clientID}c&format=json
  1. Expect a response with the JSON containing the URL to the RAW mp3
{

    "http_mp3_128_url": "https://ec-media.soundcloud.com/04NJxfCMgsq7.128.mp3?f10880d39085a94a0418a7ef69b03d522cd6dfee9399eeb9a522019b6cfab73b60ebe4206b2df5f1926659b76ff0a24fae1baf7aff77090425dc3157adfd3dd71c6f8588f2&AWSAccessKeyId=AKIAJNIGGLK7XA7YZSNQ&Expires=1407329702&Signature=lfYdnqnoK20BgQLd92sczv88xu4%3D"

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment