-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitbitApi.php
298 lines (204 loc) · 8.24 KB
/
itbitApi.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
class itbitApi
{
var $base_url,
$client_key,
$secret,
$user_id;
function __construct($apiEndpoint, $apiKey, $apiSecret, $userId){
$this->base_url = $apiEndpoint;
$this->client_key = $apiKey;
$this->secret = $apiSecret;
$this->user_id = $userId;
}
// Function that make the request header
private function makeHeaders($method, $url, $body=''){
// get current timestamp
$timestamp = time() * 1000;
// generate a nonce from timestamp + random number
$nonce = $timestamp + mt_rand(1,1000);
if($body != '') $body = json_encode($body);
$signature = $this->signMessage($method, $url, $body, $nonce, $timestamp);
return [
"Authorization: $this->client_key:$signature",
"Content-Type: application/json",
"X-Auth-Timestamp: $timestamp",
"X-Auth-Nonce: $nonce"
];
}
// Function that make the request header
private function signMessage($method, $url, $body, $nonce, $timestamp){
$message = $nonce . stripslashes(json_encode([$method, $url, addslashes($body), (string)$nonce, (string)$timestamp]));
//echo $message;
// make the hast digest using the message;
$hash_digest = hash('sha256',$message, true);
// make the hmac with the hash digest
$hmac_digest = hash_hmac('sha512', utf8_encode($url) . $hash_digest, utf8_encode($this->secret),true);
$signature = base64_encode($hmac_digest);
return $signature;
}
// function that send the request by using curl
private function sendRequest($method, $url, $body=""){
$headers = $this->makeHeaders($method, $url, $body);
//print_r($headers);
//echo $url;
if($body != "") $body = json_encode($body);
//echo $body;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method );
if($method != 'GET'){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body );
}
// return request object
$rawData = curl_exec($curl);
$info = curl_getinfo ($curl);
curl_close($curl);
//print_r($info); // uncomment this to check the request object of curl
$json = json_decode(trim($rawData));
if($json) return $json;
return trim($rawData);
}
/**
------------------------------------------------------------------------------------------------------
PUBLIC API
------------------------------------------------------------------------------------------------------
**/
// API: get tickers
public function getTickers($tickerSymbol){
$url = "$this->base_url/markets/".$tickerSymbol."/ticker";
return $this->sendRequest('GET', $url);
}
// API: get Order Book
public function getOrderBooks($tickerSymbol){
$url = "$this->base_url/markets/".$tickerSymbol."/order_book";
return $this->sendRequest('GET', $url);
}
/**
------------------------------------------------------------------------------------------------------
PRIVATE API
------------------------------------------------------------------------------------------------------
**/
// API: Create a new wallet
public function createWallet($walletName){
$post_body = [
"name"=>$walletName,
"userId"=>$this->user_id
];
$url = "$this->base_url/wallets";
return $this->sendRequest("POST", $url, $post_body);
}
// API: get all wallets
public function getAllWallets(){
$url = "$this->base_url/wallets?userId=".$this->user_id;
return $this->sendRequest('GET', $url);
}
// API: get Wallet info
public function getWallet($wallet_id){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$url = "$this->base_url/wallets/$walwallet_idlet";
return $this->sendRequest('GET', $url);
}
// API: get Wallet Balances
public function getWalletBalance($wallet_id, $currencyCode = 'USD'){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$url = "$this->base_url/wallets/$walwallet_idlet/balances/$currencyCode";
return $this->sendRequest('GET', $url);
}
// API: get Wallet Trades
public function getWalletTrades($wallet_id){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$url = "$this->base_url/wallets/$wallet_id/trades";
return $this->sendRequest('GET', $url);
}
// API: creating new Order
public function createOrder($wallet_id, $side, $type, $currency, $amount, $price, $instrument){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$order_data = [
'side' => $side,
'type' => $type,
'currency' => $currency,
'amount' => (string)$amount,
'price' => (string)$price,
'instrument' => $instrument
];
$url = "$this->base_url/wallets/$wallet_id/orders";
return $this->sendRequest('POST', $url, $order_data);
}
// API: creating new Order
public function createOrderWithDisplay($wallet_id, $side, $type, $currency, $amount, $price, $display, $instrument){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$order_data = [
'side' => $side,
'type' => $type,
'currency' => $currency,
'amount' => (string)$amount,
'price' => (string)$price,
'display' => (string)$display,
'instrument' => $instrument
];
$url = "$this->base_url/wallets/$wallet_id/orders";
return $this->sendRequest('POST', $url, $order_data);
}
// API: Get All Orders in wallet
// Remarks: Might need to create more filters ...
public function getWalletOrders($wallet_id, $instrument=null, $status=null ){
$qs = [ ]; // Querystring array
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$url = "$this->api_address/wallets/$wallet_id/orders";
if($status || $instrument) {
if($status) $qs['status'] = $status;
if($instrument) $qs['instrument'] = $instrument;
$querystring = http_build_query($qs); // build queries here
$url = "$url?$querystring";
}
return $this->sendRequest('GET', $url);
}
// API: Get specific order information
public function getOrder($orderId ,$wallet_id){
if(!$wallet) $wallet = $this->default_wallet;
$url = "$this->base_url/wallets/$wallet_id/orders/$orderId";
return $this->sendRequest('GET', $url);
}
// API: Cancel an Orders in wallet
public function cancelOrder($orderId ,$wallet_id){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$url = "$this->base_url/wallets/$wallet_id/orders/$orderId";
return $this->sendRequest("DELETE", $url);
}
// API: withdraw crypto currency
public function cryptocurrencyWithdrawalRequest($currency, $amount, $address ,$wallet_id){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$post_body = [
'currency'=>$currency,
'amount'=>$amount,
'address'=>$address
];
$url = "$this->base_url/wallets/$wallet_id/cryptocurrency_withdrawals";
return $this->sendRequest("POST", $url, $post_body);
}
// API: generate an address for user to send in cryptocurrency
public function cryptocurrencyDepositRequest($currency, $wallet_id){
if(!$wallet_id) return 'Wallet ID is missing, please enter the wallet id';
$post_body = [
'currency'=>$currency
];
$url = "$this->base_url/wallets/$wallet_id/cryptocurrency_deposits";
return $this->sendRequest("POST", $url, $post_body);
}
// API: generate an address for user to send in cryptocurrency
public function createWalletTransfer($srcWallet, $destWallet, $amount, $currCode){
$post_body = [
"sourceWalletId"=>$srcWallet,
"destinationWalletId"=>$destWallet,
"amount"=>$amount,
"currencyCode"=>$currCode
];
$url = "$this->base_url/wallet_transfers";
return $this->sendRequest("POST", $url, $post_body);
}
}# end of class