-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathRequests.php
More file actions
75 lines (67 loc) · 2.19 KB
/
Requests.php
File metadata and controls
75 lines (67 loc) · 2.19 KB
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
<?php
/**
* Performs requests on EchoNest API. API documentation should be self-explanatory.
*
* @author Brent Shaffer <bshafs at gmail dot com>
* @license MIT License
*/
class EchoNest_HttpClient_Requests extends EchoNest_HttpClient
{
/**
* Send a request to the server, receive a response
*
* @param string $apiPath Request API path
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
*
* @return string HTTP response
*/
protected function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
if($this->options['api_key'])
{
$parameters = array_merge(array(
'format' => $this->options['format'],
'api_key' => $this->options['api_key']
), $parameters);
}
$headers = array();
$headers['User-Agent'] = $this->options['user_agent'];
if ('GET' === $httpMethod && !empty($parameters)) {
$queryString = utf8_encode($this->buildQuery($parameters));
$url .= '?' . $queryString;
$this->debug('send ' . $httpMethod . ' request: ' . $url);
$request = Requests::get($url, $headers);
}
else {
$this->debug('send ' . $httpMethod . ' request: ' . $url);
$request = Requests::post($url, $headers, $parameters);
}
return $request->body;
}
protected function buildQuery($parameters)
{
$append = '';
foreach ($parameters as $key => $value)
{
// multiple parameter passed
if (is_array($value)) {
foreach ($value as $val) {
$append.=sprintf('&%s=%s', $key, $val);
}
unset($parameters[$key]);
}
elseif (is_bool($value)) {
$parameters[$key] = $value ? 'true' : 'false';
}
}
return http_build_query($parameters, '', '&') . $append;
}
protected function debug($message)
{
if($this->options['debug'])
{
print $message."\n";
}
}
}