-
Notifications
You must be signed in to change notification settings - Fork 104
/
soundcloud.php
215 lines (179 loc) · 6.69 KB
/
soundcloud.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* API Wrapper for SoundCloud written in PHP with support for authication using OAuth.
*
* @author Anton Lindqvist <[email protected]>
* @version 1.1
* @link http://github.com/mptre/php-soundcloud/
*/
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();
$this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
if (!empty($oauth_token) && !empty($oauth_token_secret)) {
$this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
} else {
$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 upload_track($post_data, $asset_data_mime = null, $artwork_data_mime = null) {
$body = '';
$boundary = '---------------------------' . md5(rand());
$crlf = "\r\n";
$headers = array(
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
'Content-Length' => 0
);
foreach ($post_data as $key => $val) {
if (in_array($key, array('track[asset_data]', 'track[artwork_data]'))) {
$body .= '--' . $boundary . $crlf;
$body .= 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($val) . '"' . $crlf;
$body .= 'Content-Type: ' . (($key == 'track[asset_data]') ? $asset_data_mime : $artwork_data_mime) . $crlf;
$body .= $crlf;
$body .= file_get_contents($val) . $crlf;
} else {
$body .= '--' . $boundary . $crlf;
$body .= 'Content-Disposition: form-data; name="' . $key .'"' . $crlf;
$body .= $crlf;
$body .= $val . $crlf;
}
}
$body .= '--' . $boundary . '--' . $crlf;
$headers['Content-Length'] += strlen($body);
return $this->request('tracks', 'POST', $body, $headers);
}
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();
if(!isset($headers['Content-Length'])) {
$headers['Content-Length'] = 0;
}
$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;
}
}