Skip to content

Commit 86da724

Browse files
Added a new sandbox api class
1 parent d52469d commit 86da724

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

lib/EchoNest/Api/Sandbox.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
4+
Class SignatureCreation {
5+
6+
public static function oauth_hmacsha1($key, $data) {
7+
return base64_encode(self::hmacsha1($key, $data));
8+
}
9+
10+
public static function hmacsha1($key,$data) {
11+
$blocksize=64;
12+
$hashfunc='sha1';
13+
if (strlen($key)>$blocksize)
14+
$key=pack('H*', $hashfunc($key));
15+
$key=str_pad($key,$blocksize,chr(0x00));
16+
$ipad=str_repeat(chr(0x36),$blocksize);
17+
$opad=str_repeat(chr(0x5c),$blocksize);
18+
$hmac = pack(
19+
'H*',$hashfunc(
20+
($key^$opad).pack(
21+
'H*',$hashfunc(
22+
($key^$ipad).$data
23+
)
24+
)
25+
)
26+
);
27+
return $hmac;
28+
}
29+
30+
public static function oauth_signature($url,$params, $secret) {
31+
32+
// sort the parameters
33+
ksort($params);
34+
35+
$param_string = "";
36+
$i = 0;
37+
foreach ($params as $k => $v) {
38+
if ($i != 0) {
39+
$param_string .= "&";
40+
}
41+
if (is_array($v)) {
42+
$j = 0;
43+
foreach($v as $key => $val) {
44+
if ($j != 0) {
45+
$param_string .= "&";
46+
}
47+
$param_string .= $k . "=" . $val;
48+
$j++;
49+
}
50+
} else {
51+
$param_string .= $k . "=" . $v;
52+
}
53+
$i++;
54+
}
55+
56+
57+
echo $param_string."<br/>";
58+
59+
$data = "GET&".urlencode($url). "&".urlencode($param_string);
60+
61+
62+
$key = $secret."&";
63+
64+
$sig = self::oauth_hmacsha1($key, $data);
65+
return $sig;
66+
}
67+
}
68+
69+
70+
/**
71+
* API calls for managing sandboxes
72+
*
73+
* @link http://developer.echonest.com/docs/v4/catalog.html#overview
74+
* @author Syd Lawrence <sydlawrence at gmail dot com>
75+
* @license MIT License
76+
*/
77+
class EchoNest_Api_Sandbox extends EchoNest_Api
78+
{
79+
80+
protected $sandbox = "";
81+
82+
protected $oauth_config = array
83+
(
84+
"consumer_key" => "",
85+
"consumer_secret" => "",
86+
);
87+
88+
function setSandbox($sandbox)
89+
{
90+
if ($sandbox)
91+
$this->sandbox = $sandbox;
92+
return $this;
93+
}
94+
95+
function setConfig($oauth_config)
96+
{
97+
foreach ($this->oauth_config as $key => $val) {
98+
99+
if (!isset($oauth_config[$key]) || $oauth_config[$key] == "") {
100+
// @todo make this thrown an exception
101+
throw new Exception('Missing sandbox oauth config: '.$key);
102+
}
103+
$this->oauth_config[$key] = $oauth_config[$key];
104+
}
105+
return $this;
106+
107+
}
108+
109+
110+
/**
111+
* Lists assets in a sandbox.
112+
* http://developer.echonest.com/docs/v4/catalog.html#create
113+
*
114+
* @param string $name The name of the catalog
115+
* @param string $type The type of the catalog (artist or song)
116+
* @return array response object
117+
*/
118+
function assets($start = 0, $per_page=100)
119+
{
120+
121+
$response = $this->client->get('sandbox/list', array(
122+
'sandbox' => $this->sandbox,
123+
'results' => $per_page,
124+
'start' => $start
125+
));
126+
127+
return $this->returnResponse($response);
128+
}
129+
130+
function fetch($id)
131+
{
132+
133+
$endpoint = "sandbox/access";
134+
135+
$time = time();
136+
$params = array(
137+
"api_key" => $this->client->getHttpClient()->getOption('api_key'),
138+
"id" => $id,
139+
"oauth_nonce" => md5($time),
140+
"oauth_timestamp" => $time,
141+
"format" => $this->client->getHttpClient()->getOption('format'),
142+
"oauth_signature_method" => "HMAC-SHA1",
143+
"oauth_version" => "1.0",
144+
"oauth_consumer_key" => $this->oauth_config['consumer_key'],
145+
"sandbox" => $this->sandbox
146+
);
147+
148+
$url = strtr($this->client->getHttpClient()->getOption('url'), array(
149+
':api_version' => $this->client->getHttpClient()->getOption('api_version'),
150+
':protocol' => $this->client->getHttpClient()->getOption('protocol'),
151+
':path' => trim($endpoint, '/')
152+
));
153+
154+
$sig = SignatureCreation::oauth_signature($url,$params, $this->oauth_config['consumer_secret']);
155+
156+
$params['oauth_signature'] = $sig;
157+
158+
159+
$response = $this->client->get($endpoint, $params);
160+
161+
return $this->returnResponse($response);
162+
163+
}
164+
}

0 commit comments

Comments
 (0)