-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
219 lines (185 loc) · 5.37 KB
/
api.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
216
217
218
219
<?php
require 'keys.php';
// Pull in JSON
// http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data
$params = json_decode(file_get_contents('php://input'),true);
/**
* Return a failure message
* @param unknown $message
*/
function setErrorHeader($message){
http_response_code(404);
header('Content-Type: application/json');
echo $message;
}
/**
* Return a success message
* @param unknown $message
*/
function setSuccessHeader($message){
http_response_code(200);
header('Content-Type: application/json');
echo $message;
}
/**
* Configure a curl object
* @return unknown
*/
function setUpRdioCurl($clientId, $data){
// Set the URI during init
$ch = curl_init(RDIO_TOKEN_URI);
// Set auth
// http://www.rdio.com/developers/docs/web-service/oauth2/overview/ref-oauth2-client-verification
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.':'.RDIO_CLIENT_SECRET);
// Set Content-Type
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
// Capture the json data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Allow the header to be dumped
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// Set POST Data
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
return $ch;
}
/**
* Get a new token
* @param unknown $redirectUri
* @param unknown $clientId
* @param unknown $code
*/
function getRdioToken($redirectUri, $clientId, $code){
$result = null;
$error = false;
$ch = setUpRdioCurl($clientId, [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirectUri
]);
// Run query
$result = curl_exec($ch);
// Something failed during the request
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400){
$error = true;
}
curl_close($ch);
if($error) return setErrorHeader($result);
// Return our token data
return setSuccessHeader($result);
}
/**
* Get a new token with a refresh token
* @param unknown $refreshToken
*/
function getRdioRefreshToken($clientId, $refreshToken){
$result = null;
$error = false;
$ch = $ch = setUpRdioCurl($clientId, [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken
]);
// Run query
$result = curl_exec($ch);
// Something failed during the request
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400){
$error = true;
}
curl_close($ch);
if($error) return setErrorHeader($result);
// Return our token data
return setSuccessHeader($result);
}
/**
* Set up curl request
* @param unknown $clientId
* @param unknown $data
*/
function setUpSpotifyCurl($clientId, $data){
// Set the URI during init
$ch = curl_init(SPOTIFY_TOKEN_URI);
// Set auth
// https://developer.spotify.com/web-api/authorization-guide/
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.':'.SPOTIFY_CLIENT_SECRET);
// Set Content-Type
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
// Capture the json data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Allow the header to be dumped
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// Set POST Data
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
return $ch;
}
// Go get the spotify token
/**
* Get a token from spotify
* @param unknown $redirectUri
* @param unknown $clientId
* @param unknown $code
*/
function getSpotifyToken($redirectUri, $clientId, $code){
$result = null;
$error = false;
$ch = setUpSpotifyCurl($clientId, [
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirectUri
]);
// Run query
$result = curl_exec($ch);
// Something failed during the request
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400){
$error = true;
}
curl_close($ch);
if($error) return setErrorHeader($result);
// Return our token data
return setSuccessHeader($result);
}
/**
* Get refresh token for spotify
* @param unknown $clientId
* @param unknown $refreshToken
*/
function getSpotifyRefreshToken($clientId, $refreshToken){
$result = null;
$error = false;
$ch = $ch = setUpSpotifyCurl($clientId, [
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken
]);
// Run query
$result = curl_exec($ch);
// Something failed during the request
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400){
$error = true;
}
curl_close($ch);
if($error) return setErrorHeader($result);
// Return our token data
return setSuccessHeader($result);
}
// Figure out what we need to do
switch($params['client']){
case 'rdio':
if($params['task'] == 'refresh_token'){
return getRdioRefreshToken($params['clientId'], $params['refresh_token']);
}
elseif($params['task'] == 'token'){
return getRdioToken($params['redirectUri'], $params['clientId'], $params['code']);
}
case 'spotify':
if($params['task'] == 'refresh_token'){
return getSpotifyRefreshToken($params['clientId'], $params['refresh_token']);
}
elseif($params['task'] == 'token'){
return getSpotifyToken($params['redirectUri'], $params['clientId'], $params['code']);
}
return getSpotifyToken($params['code']);
default:
return setErrorHeader(json_encode(['error' => 'No client set']));
}
?>