-
Notifications
You must be signed in to change notification settings - Fork 1
/
Abstract.php
42 lines (35 loc) · 1.2 KB
/
Abstract.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
<?php
abstract class PhEchonest_Abstract
{
protected static $_apiKey = 'XXQUZTRTJZWLMLINE';
protected static $_format = 'json';
protected static $baseUrl = 'http://developer.echonest.com/api/v4';
public static function setApiKey($apiKey)
{
self::$_apiKey = $apiKey;
}
protected static function makeRequest($method, $query = array())
{
$url = self::buildRequestUrl($method, $query);
$result = Zend_Json::decode(file_get_contents($url));
if ($result['response']['status']['message'] != 'Success') {
global $log;
$log->debug($url);
$log->debug($result);
throw new Exception('API request failed');
}
unset($result['response']['status']);
return $result['response'];
}
protected static function buildRequestUrl($method, $query)
{
$url = static::$baseUrl . '/'.static::$_resource.'/'.$method.'?api_key='.static::$_apiKey;
foreach ($query as $key => $value) {
$key = urlencode($key);
$value = urlencode($value);
$url .= "&{$key}={$value}";
}
//echo $url;
return $url;
}
}