1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Njasm\Soundcloud; |
4
|
|
|
|
5
|
|
|
use Njasm\Soundcloud\Resource\ResourceInterface; |
6
|
|
|
|
7
|
|
|
use Njasm\Soundcloud\UrlBuilder\UrlBuilderInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* SoundCloud API wrapper in PHP |
11
|
|
|
* |
12
|
|
|
* @author Nelson J Morais <[email protected]> |
13
|
|
|
* @copyright 2014 Nelson J Morais <[email protected]> |
14
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
15
|
|
|
* @link http://github.com/njasm/soundcloud |
16
|
|
|
* @package Njasm\Soundcloud |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class SoundcloudFacade extends Soundcloud |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Get the authorization url for your users. |
23
|
|
|
* |
24
|
|
|
* @param array $params key => value pair, of params to be sent to the /connect endpoint. |
25
|
|
|
* @return string The URL |
26
|
|
|
*/ |
27
|
1 |
|
public function getAuthUrl(array $params = array()) |
28
|
|
|
{ |
29
|
|
|
$defaultParams = array( |
30
|
1 |
|
'client_id' => $this->auth->getClientID(), |
31
|
1 |
|
'scope' => 'non-expiring', |
32
|
1 |
|
'display' => 'popup', |
33
|
1 |
|
'response_type' => 'code', |
34
|
1 |
|
'redirect_uri' => $this->auth->getAuthUrlCallback(), |
35
|
1 |
|
'state' => '' |
36
|
|
|
); |
37
|
|
|
|
38
|
1 |
|
$params = array_merge($defaultParams, $params); |
39
|
1 |
|
$resource = $this->make(ResourceInterface::class, array('get', '/connect', $params)); |
40
|
1 |
|
$url = $this->make(UrlBuilderInterface::class, array($resource, 'www')); |
41
|
|
|
|
42
|
1 |
|
return $url->getUrl(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Request for a valid access token via User Credential Flow |
47
|
|
|
* |
48
|
|
|
* @param string $username user username |
49
|
|
|
* @param string $password user password |
50
|
|
|
* @return \Njasm\Soundcloud\Request\ResponseInterface |
51
|
|
|
*/ |
52
|
1 |
|
public function userCredentials($username, $password) |
53
|
|
|
{ |
54
|
|
|
$defaultParams = array( |
55
|
1 |
|
'grant_type' => 'password', |
56
|
1 |
|
'scope' => 'non-expiring', |
57
|
1 |
|
'username' => $username, |
58
|
1 |
|
'password' => $password |
59
|
|
|
); |
60
|
|
|
|
61
|
1 |
|
$params = $this->mergeAuthParams($defaultParams, true); |
62
|
1 |
|
$response = $this->post('/oauth2/token', $params)->asJson() |
|
|
|
|
63
|
1 |
|
->request(array( |
64
|
1 |
|
CURLOPT_FOLLOWLOCATION => true, |
65
|
1 |
|
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded') |
66
|
1 |
|
))->bodyObject(); |
67
|
1 |
|
$this->setAuthData($response); |
68
|
|
|
|
69
|
1 |
|
return $this->response; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Second step in user authorization. |
74
|
|
|
* Exchange code for token |
75
|
|
|
* |
76
|
|
|
* @param string $code the code received to exchange for token |
77
|
|
|
* @param array $params |
78
|
|
|
* @return \Njasm\Soundcloud\Request\ResponseInterface |
79
|
|
|
*/ |
80
|
1 |
|
public function codeForToken($code, array $params = array()) |
81
|
|
|
{ |
82
|
|
|
$defaultParams = array( |
83
|
1 |
|
'redirect_uri' => $this->auth->getAuthUrlCallback(), |
84
|
1 |
|
'grant_type' => 'authorization_code', |
85
|
1 |
|
'code' => $code |
86
|
|
|
); |
87
|
|
|
|
88
|
1 |
|
$mergedParams = array_merge($defaultParams, $params); |
89
|
1 |
|
$finalParams = $this->mergeAuthParams($mergedParams, true); |
90
|
1 |
|
$response = $this->post('/oauth2/token', $finalParams)->asJson() |
|
|
|
|
91
|
1 |
|
->request(array( |
92
|
1 |
|
CURLOPT_FOLLOWLOCATION => true, |
93
|
1 |
|
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded') |
94
|
1 |
|
))->bodyObject(); |
95
|
1 |
|
$this->setAuthData($response); |
96
|
|
|
|
97
|
1 |
|
return $this->response; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Refresh Auth access token. |
102
|
|
|
* |
103
|
|
|
* @param string|null $refreshToken the refresh token to send to soundcloud. if null, the default Auth object |
104
|
|
|
* refresh token will be used. |
105
|
|
|
* @param array $params |
106
|
|
|
* @return \Njasm\Soundcloud\Request\ResponseInterface |
107
|
|
|
*/ |
108
|
1 |
|
public function refreshAccessToken($refreshToken = null, array $params = array()) |
109
|
|
|
{ |
110
|
|
|
$defaultParams = array( |
111
|
1 |
|
'redirect_uri' => $this->auth->getAuthUrlCallback(), |
112
|
1 |
|
'client_id' => $this->auth->getClientID(), |
113
|
1 |
|
'client_secret' => $this->auth->getClientSecret(), |
114
|
1 |
|
'grant_type' => 'refresh_token', |
115
|
1 |
|
'refresh_token' => ($refreshToken) ?: $this->auth->getRefreshToken() |
116
|
|
|
); |
117
|
|
|
|
118
|
1 |
|
$finalParams = array_merge($defaultParams, $params); |
119
|
1 |
|
$response = $this->post('/oauth2/token', $finalParams)->asJson() |
|
|
|
|
120
|
1 |
|
->request(array( |
121
|
1 |
|
CURLOPT_FOLLOWLOCATION => true, |
122
|
1 |
|
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded') |
123
|
1 |
|
))->bodyObject(); |
124
|
1 |
|
$this->setAuthData($response); |
125
|
|
|
|
126
|
1 |
|
return $this->response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets OAuth data received from Soundcloud into Auth object. |
131
|
|
|
* |
132
|
|
|
* @param \stdClass $response |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
3 |
|
protected function setAuthData($response) |
136
|
|
|
{ |
137
|
3 |
|
$accessToken = isset($response->access_token) ? $response->access_token : null; |
138
|
3 |
|
$scope = isset($response->scope) ? $response->scope : null; |
139
|
3 |
|
$expires = isset($response->expires_in) ? $response->expires_in : null; |
140
|
3 |
|
$refreshToken = isset($response->refresh_token) ? $response->refresh_token : null; |
141
|
|
|
|
142
|
3 |
|
$this->auth->setToken($accessToken); |
143
|
3 |
|
$this->auth->setScope($scope); |
144
|
3 |
|
$this->auth->setExpires($expires); |
145
|
3 |
|
$this->auth->setRefreshToken($refreshToken); |
146
|
3 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Download a track from soundcloud. |
150
|
|
|
* |
151
|
|
|
* @param integer track ID. |
152
|
|
|
* @param boolean $download if we should follow location and download the media file to an in-memory variable |
153
|
|
|
* accessible on the Response::bodyRaw() method, or return the Response object with the |
154
|
|
|
* location header with the direct URL. |
155
|
|
|
* @return \Njasm\Soundcloud\Request\ResponseInterface |
156
|
|
|
*/ |
157
|
1 |
|
public function download($trackID, $download = false) |
158
|
|
|
{ |
159
|
1 |
|
$path = '/tracks/' . intval($trackID) . '/download'; |
160
|
1 |
|
$this->get($path); |
161
|
|
|
|
162
|
1 |
|
if ($download === true) { |
163
|
1 |
|
$this->request(array(CURLOPT_FOLLOWLOCATION => true)); |
164
|
|
|
} else { |
165
|
1 |
|
$this->request(array(CURLOPT_FOLLOWLOCATION => false)); |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
return $this->response; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Upload a track to soundcloud. |
173
|
|
|
* |
174
|
|
|
* @param string $trackPath the path to the media file to be uploaded to soundcloud. |
175
|
|
|
* @param array $params the params/info for the track that will be uploaded like, licence, name, etc. |
176
|
|
|
* @return \Njasm\Soundcloud\Request\ResponseInterface |
177
|
|
|
*/ |
178
|
1 |
|
public function upload($trackPath, array $params = array()) |
179
|
|
|
{ |
180
|
|
|
// loop to keep BC. params array can be |
181
|
|
|
// array('track[title]' => 'track name', ...) or |
182
|
|
|
// array('title' => 'track name', 'downloadable' => true, ...) |
183
|
1 |
|
foreach($params as $key => $value) { |
184
|
|
|
if (stripos($key, 'track[') !== false) { |
185
|
|
|
continue; |
186
|
|
|
} |
187
|
|
|
$params['track[' . $key . ']'] = $value; |
188
|
|
|
unset($params[$key]); |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
$file = $this->getCurlFile($trackPath); |
192
|
1 |
|
$params = array_merge($params, array('track[asset_data]' => $file)); |
193
|
1 |
|
$finalParams = $this->mergeAuthParams($params); |
194
|
|
|
|
195
|
1 |
|
return $this->post('/tracks')->setParams($finalParams) |
196
|
1 |
|
->request(array(CURLOPT_HTTPHEADER => array('Content-Type: multipart/form-data'))); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $trackPath the full path for the media file to upload. |
201
|
|
|
* @return string|\CURLFile object if CurlFile class available or string prepended with @ for deprecated file upload. |
202
|
|
|
*/ |
203
|
1 |
|
private function getCurlFile($trackPath) |
204
|
|
|
{ |
205
|
1 |
|
if (class_exists('CurlFile') === true) { |
206
|
1 |
|
return new \CURLFile($trackPath); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return "@" . $trackPath; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.