-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBit2cClient.php
211 lines (171 loc) · 5.41 KB
/
Bit2cClient.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
<?php
require_once('Enums.php');
require_once('Objects.php');
class Bit2cClient{
private $Key;
private $Secret;
private $Url;
public function Bit2cClient($url , $key, $secret){
$this->Key = $key;
$this->Url = $url;
$this->Secret = strtoupper($secret);
}
public function GetQueryString($obj){
$params['nonce'] = (time());
if($obj != null)
foreach( get_object_vars($obj) as $field_name => $field_value ){
$params[$field_name] = $field_value;
}
$flag = false;
$str = '';
foreach( $params as $key => $val ){
if( !$flag )
$flag = true;
else
$str .= '&';
if($key == 'IsBid')
{
if( strlen($val) == 0 )
$val = 'false';
else
$val = 'true';
}
$str .= "{$key}={$val}";
}
return ($str);
}
private function ComputeHash( $message ){
return base64_encode(hash_hmac('sha512', $message, $this->Secret , true));
}
private function Query( $object , $url ){
$qString = $this->GetQueryString($object);
$sign = $this->ComputeHash($qString);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL , $url);
curl_setopt($curl,CURLOPT_HTTPHEADER,array("Key:{$this->Key}", "Sign:{$sign}"));
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl,CURLOPT_POSTFIELDS,$qString);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($curl);
return $res;
}
private function CastObject($obj, $res){
foreach( get_object_vars($res) as $field_name => $field_value ){
$obj->$field_name = $field_value;
}
return $obj;
}
public function GetTrades($pair = PairType::BtcNis, $since = 0, $date = 0){
$json = json_decode(file_get_contents("{$this->Url}Exchanges/{$pair}/trades.json"));
foreach($json as $trade){
$Trade = new ExchangesTrade();
$Trade = $this->CastObject($Trade, $trade);
$Trades[] = $Trade;
}
return $Trades;
}
public function GetTicker($pair = PairType::BtcNis){
$json = json_decode(file_get_contents("{$this->Url}Exchanges/{$pair}/Ticker.json"));
$ticker = new Ticker();
$ticker = $this->CastObject($ticker, $json);
return $ticker;
}
public function GetOrderBook($pair = PairType::BtcNis){
$json = json_decode(file_get_contents("{$this->Url}Exchanges/{$pair}/orderbook.json"));
$orderBook = new OrderBook();
foreach($json->bids as $bid){
$bids[] = $bid;
}
foreach($json->asks as $ask){
$asks[] = $ask;
}
$orderBook->bids = $bids;
$orderBook->asks=$asks;
return $orderBook;
}
public function AddOrder($orderData){
$url = "{$this->Url}Order/AddOrder";
$json = json_decode($this->Query($orderData, $url));
$orderRes = new OrderResponse();
$od = new od();
$od = $this->CastObject($od, $json->NewOrder);
$orderRes = $this->CastObject($orderRes, $json->OrderResponse);
$addOrderRes = new AddOrderResponse();
$addOrderRes->OrderResponse = $orderRes;
$addOrderRes->NewOrder = $od;
return $addOrderRes;
}
public function MyOrders($pair = PairType::BtcNis){
$obj = new stdClass();
$obj->pair = $pair;
$url = "{$this->Url}Order/MyOrders";
$json = json_decode($this->Query($obj, $url));
$orders = new Orders();
if(!empty($json->bids)){
foreach($json->bids as $bid){
$tradeOrder = new TradeOrder();
$tradeOrder = $this->CastObject($tradeOrder, $bid);
$bids[] = $tradeOrder;
}
$orders->bids = $bids;
}
if(!empty($json->asks)){
foreach($json->asks as $ask){
$tradeOrder = new TradeOrder();
$tradeOrder = $this->CastObject($tradeOrder, $ask);
$asks[] = $tradeOrder;
}
$orders->asks = $asks;
}
return $orders;
}
public function AccountHistory($fromTime=0, $toTime=0){
$obj = new stdClass();
$obj->fromTime = $fromTime;
$obj->toTime = $toTime;
$url = "{$this->Url}Order/AccountHistory";
$json = json_decode($this->Query($obj, $url));
foreach($json as $raw){
$accountRaw = new AccountRaw();
$accountRaw = $this->CastObject($accountRaw, $raw);
$accountRaws[] = $accountRaw;
}
return isset($accountRaws)? $accountRaws : null;
}
public function CancelOrder($id){
$obj = new stdClass();
$obj->id = $id;
$url = "{$this->Url}Order/CancelOrder";
$json = json_decode($this->Query($obj, $url));
$orderResponse = new OrderResponse();
$orderResponse = $this->CastObject($orderResponse, $json);
return $orderResponse;
}
public function ClearMyOrders($pair = PairType::BtcNis){
$orders = $this->MyOrders();
foreach($orders as $type => $val){
if(!empty($val))
foreach($val as $order){
if($order->pair == $pair)
$this->CancelOrder($order->id);
}
}
}
public function Balance(){
$url = "{$this->Url}Account/Balance";
$response = $this->Query(null, $url);
$json = json_decode($response);
$balance = new UserBalance();
$balance = $this->CastObject($balance, $json);
return $balance;
}
public function CreateCheckout($data){
$url = "{$this->Url}Merchant/CreateCheckout";
$json = json_decode($this->Query($data, $url));
$CheckoutResponse = new CheckoutResponse();
$CheckoutResponse = $this->CastObject($CheckoutResponse , $json);
return $CheckoutResponse;
}
}
?>