/
ApiRequest.php
172 lines (144 loc) · 3.7 KB
/
ApiRequest.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
<?php
class ApiRequestException extends Exception {}
class ApiRequest {
/**
* API client id
*
* @var mixed
*/
protected $clientId;
/**
* API secret key
*
* @var string
*/
protected $secretKey;
/**
* API endpoint URL
*
* @var type
*/
protected $endPoint;
/**
* Set client id
*
* @param mixed $id
* @return ApiRequest
*/
public function setClientId($id) {
$this->clientId = $id;
return $this;
}
/**
* Get client id
*
* @return mixed
*/
public function getClientId() {
return $this->clientId;
}
/**
* Set secret key
*
* @param string $key
* @return ApiRequest
*/
public function setSecretKey($key) {
$this->secretKey = $key;
return $this;
}
/**
* Get secret key
*
* @return string
*/
public function getSecretKey() {
return $this->secretKey;
}
/**
* Set API endpoint URL
*
* @param string $url
* @return ApiRequest
*/
public function setEndpointUrl($url) {
$this->endPoint = $url;
return $this;
}
/**
* Get API endpoint URL
*
* @return string
*/
public function getEndPoint() {
return $this->endPoint;
}
/**
* Make API call with all input data
*
* @param string $uri
* @param string $method
* @param array $data
* @return string
*/
public function make($uri, $method = 'GET', $data = array()) {
$url = $this->endPoint . $uri;
$params = $this->getParams($uri, $data);
return json_decode($this->call($url, $params, strtoupper($method)), true);
}
/**
* Get prepared inputs with hash
*
* @param type $uri
* @param type $data
* @return array
* @throws ApiRequestException
*/
public function getParams($uri, $data = array()) {
// check for reserved keys
if (!empty($data)) {
foreach (array('uri', 'client_id', 'time', 'hash') as $arg) {
if (array_key_exists($arg, $data)) {
throw new ApiRequestException("You can't use '$arg' parameter, it is reserved");
}
}
}
// merge input data and generated parameters
$data = array_merge($data, array(
'uri' => $uri,
'client_id' => $this->clientId,
'time' => strtotime(gmdate('d.m.Y H:i:s')),
));
$hash = Hash::make($data, $this->secretKey);
// return merged parameters with hash
return array_merge($data, array('hash' => $hash));
}
/**
* Make cURL call
*
* @param string $url
* @param array $params
* @param string $method
* @return string
* @throws ApiRequestException
*/
protected function call($url, array $params, $method = 'POST') {
$ch = curl_init();
if ($method == 'GET') {
$url .= '?' . http_build_query($params);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$output = curl_exec($ch);
if ($output === false || curl_errno($ch)) {
throw new ApiRequestException('cURL error: ' . curl_error($ch));
}
curl_close($ch);
return $output;
}
}