nguyenvanduocit
6/15/2013 - 2:11 PM

get stream URL from soundcloud

get stream URL from soundcloud

<?php
	class SoundcloudAPI
	{
		protected $clientID;
	    function __construct() {
	        $this->clientID = "69b6a6bc4f7d483fd21b170137a9cd51";
	    }
	    
	    function GetTrackLink($id)
	    {
	    	$trackinforURL = "http://api.soundcloud.com/tracks/$id.json?client_id=69b6a6bc4f7d483fd21b170137a9cd51";
	    	return json_decode($this->getPageData($trackinforURL));
	    }
	    function getPageData($url, $follow = TRUE, $cookie = FALSE)
		{
		    $cookie_file_path = 'cookies.txt';
		    $ch = curl_init();
		    curl_setopt($ch, CURLOPT_URL, $url);
		    curl_setopt($ch, CURLOPT_HEADER, 0);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $follow);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		    curl_setopt($ch, CURLOPT_FILETIME, 1);
		    curl_setopt($ch, CURLOPT_TIMEOUT, 0);
		    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
		    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

		    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; it; rv:2.0b4) Gecko/20100818');
		    curl_setopt($ch, CURLOPT_REFERER, $url);
		    if($cookie)
		        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
		    
		    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);

		    $data = curl_exec($ch);
		    curl_close($ch);
		    
		    return $data;
		}
		function getStreamURL($id)
		{
			$track = $this->GetTrackLink("oh-love-01");
			return $track->stream_url."?client_id=".$this->clientID;
		}
	}
<?php
	require_once 'Soundcloud.class.php';
	if(isset($_GET["id"]))
	{
		$demo = new SoundcloudAPI();
		header ('Location: '.$demo->getStreamURL($_GET["id"]));
	}
?>