* @version 1.1
* @link http://github.com/mptre/php-soundcloud/
*/
// desbest edit. uncomment the lines below
//if the api fails to generate a token
//one line at a time. working DoWnWaRdS
//echo "$consumer_key
$consumer_secret,
";
// echo "$oauth_token
$oauth_token_secret";
//exit();
//
class Soundcloud {
const VERSION = '1.1';
const URL_API = 'http://api.soundcloud.com/';
const URL_OAUTH = 'http://api.soundcloud.com/oauth/';
function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
$this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1() or die ("could not sha1 method");
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret)or die ("could not make a consumer");
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
} else {
echo "This yellow section is coming from soundcloud.php
A token could not be generated.
$oauth_token and $oauth_token_secret
";
$this->token = NULL;
}
}
function get_authorize_url($token) {
if (is_array($token)) {
$token = $token['oauth_token'];
}
return $this->_get_url('authorize') . sprintf('?oauth_token=%s', $token);
}
function get_request_token($oauth_callback) {
$request = $this->request(
$this->_get_url('request'),
'POST',
array('oauth_callback' => $oauth_callback)
);
$token = $this->_parse_response($request);
$this->token = new OAuthConsumer(
$token['oauth_token'],
$token['oauth_token_secret']
);
return $token;
}
function get_access_token($token) {
$response = $this->request(
$this->_get_url('access'),
'POST',
array('oauth_verifier' => $token)
);
$token = $this->_parse_response($response);
$this->token = new OAuthConsumer(
$token['oauth_token'],
$token['oauth_token_secret']
);
return $token;
}
function request($resource, $method = 'GET', $args = array(), $headers = NULL) {
if (!preg_match('/http:\/\//', $resource)) {
$url = self::URL_API . $resource;
} else {
$url = $resource;
}
if (stristr($headers['Content-Type'], 'multipart/form-data')) {
$body = FALSE;
} elseif (stristr($headers['Content-Type'], 'application/xml')) {
$body = FALSE;
} else {
$body = TRUE;
}
$request = OAuthRequest::from_consumer_and_token(
$this->consumer,
$this->token,
$method,
$url,
($body === TRUE) ? $args : NULL
);
$request->sign_request($this->sha1_method, $this->consumer, $this->token);
return $this->_curl(
$request->get_normalized_http_url(),
$request,
$args,
$headers
);
}
private function _build_header($headers) {
$h = array();
if (count($headers) > 0) {
foreach ($headers as $key => $val) {
$h[] = $key . ': ' . $val;
}
return $h;
} else {
return $headers;
}
}
private function _curl($url, $request, $post_data = NULL, $headers = NULL) {
$ch = curl_init();
$mime = (stristr($headers['Content-Type'], 'multipart/form-data')) ? TRUE : FALSE;
$headers['User-Agent'] = (isset($headers['User-Agent']))
? $headers['User-Agent']
: 'PHP SoundCloud/' . self::VERSION;
$headers = (is_array($headers)) ? $this->_build_header($headers) : array();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => FALSE,
CURLOPT_RETURNTRANSFER => TRUE
);
if (in_array($request->get_normalized_http_method(), array('DELETE', 'PUT'))) {
$options[CURLOPT_CUSTOMREQUEST] = $request->get_normalized_http_method();
$options[CURLOPT_POSTFIELDS] = '';
}
if (is_array($post_data) && count($post_data) > 0 || $mime === TRUE) {
$options[CURLOPT_POSTFIELDS] = (is_array($post_data) && count($post_data) == 1)
? ((isset($post_data[0])) ? $post_data[0] : $post_data)
: $post_data;
$options[CURLOPT_POST] = TRUE;
}
$headers[] = $request->to_header();
$options[CURLOPT_HTTPHEADER] = $headers;
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $response;
}
private function _get_url($type) {
switch ($type) {
case 'access':
$method = 'access_token';
break;
case 'authorize':
$method = 'authorize';
break;
case 'request':
$method = 'request_token';
break;
}
return self::URL_OAUTH . $method;
}
private function _parse_response($response) {
$return = array();
$response = explode('&', $response);
foreach ($response as $r) {
if (strstr($r, '=')) {
list($key, $val) = explode('=', $r);
if (!empty($key) && !empty($val)) {
$return[urldecode($key)] = urldecode($val);
}
}
}
return (count($return) > 0) ? $return : FALSE;
}
}