-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathitbit.php
More file actions
122 lines (101 loc) · 4.04 KB
/
itbit.php
File metadata and controls
122 lines (101 loc) · 4.04 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
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
<?php
###############################################
# itbit-php Class 1.0.1
# Author: David Barnes
# Contact: david.barnes (at) bitcoin.co.th
# Copyright (c) 2015 Bitcoin Co. Ltd.
# License: MIT
###############################################
class itbit{
var $api_url = 'https://api.itbit.com/v1/';
var $secret, $client, $user_id;
function __construct($secret, $client, $user_id){
$this->secret = $secret;
$this->client = $client;
$this->user_id = $user_id;
}
private function curl($url, $body = '', $type=''){
$url = $this->api_url.$url;
// Generate a nonce
$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 6);
// Use current timestamp
$timestamp = time() * 1000;
if($body != ''){
$body = json_encode($body);
}
$signature = $this->sign_message(($type != '' ? $type : ($body == '' ? 'GET' : 'POST')),$url, $body, $nonce, $timestamp);
$headers = array('Authorization: '.$this->client.':'.$signature,
'X-Auth-Timestamp: '.$timestamp,
'X-Auth-Nonce: '. $this->nformat($nonce),
'User-Agent: php-requester',
'Connection: keep-alive',
'Accept-Encoding: gzip, deflate',
'Content-Type: application/json');
$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, ($type != '' ? $type : ($body == '' ? 'GET' : 'POST')));
if($body != ''){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
}
$rawData = curl_exec($curl);
$info = curl_getinfo ($curl);
curl_close($curl);
if($json = json_decode(trim($rawData))){
return $json;
}
return trim($rawData);
}
private function sign_message($verb, $url, $body, $nonce, $timestamp){
//Generate the message
$message = stripslashes(json_encode(array($verb, $url, ($body == '' ? '' : addslashes($body)), (string)$nonce, (string)$timestamp)));
// Hash the message plus nonce
$nonced_message = $this->nformat($nonce) . $message;
$hash_digest = hash('sha256',$nonced_message, true);
$hmac_digest = hash_hmac('sha512', utf8_encode($url) . $hash_digest, utf8_encode($this->secret),true);
$sig = base64_encode($hmac_digest);
return $sig;
}
// Make sure the nonce doesn't get put into notation
private function nformat($nonce){
return number_format($nonce,0,'','');
}
// Below are the public methods that should be used to interact with the API
public function wallet($wallet_id='', $currency = ''){
return $this->curl('wallets'.($wallet_id != '' ? '/'.$wallet_id . ($currency != '' ? '/balances/'.$currency : '') : '?userId='.$this->user_id));
}
public function balance($wallet_id, $currency){
return $this->wallet($wallet_id, $currency);
}
public function orders($wallet_id, $order_id=''){
return $this->curl('wallets/'.$wallet_id.'/orders'.($order_id != '' ? '/'.$order_id : ''));
}
public function trades($wallet_id){
return $this->curl('wallets/'.$wallet_id.'/trades');
}
public function cancel($wallet_id, $order_id){
return $this->curl('wallets/'.$wallet_id.'/orders/'.$order_id,'','DELETE');
}
public function create_order($wallet_id, $order_type, $amount, $price){
$order_data = array('side' => ($order_type == 'sell' ? 'sell' : 'buy'),
'type' => 'limit',
'currency' => 'XBT',
'amount' => (string)number_format($amount,4,'.',''),
'price' => (string)$price,
'instrument' => 'XBTUSD');
return $this->curl('wallets/'.$wallet_id.'/orders',$order_data,'POST');
}
public function withdraw($wallet_id, $amount, $address){
$withdraw_data = array('currency' => 'XBT',
'amount' => (string)$amount,
'address' => $address);
return $this->curl('wallets/'.$wallet_id.'/cryptocurrency_withdrawals',$withdraw_data,'POST');
}
public function deposit($wallet_id){
return $this->curl('wallets/'.$wallet_id.'/cryptocurrency_deposits',array('currency' => 'XBT'),'POST');
}
}