· 5 years ago · Aug 13, 2020, 06:34 AM
1<?php
2
3class API {
4
5 protected $base = 'https://api-adapter.backend.currency.com';
6 protected $api_key;
7 protected $api_secret;
8 protected $info = array(
9 "timeOffset" => 0
10 );
11 public $httpDebug = false;
12 public $balances = array();
13 public $btc_value = 0.00;
14 public $btc_total = 0.00;
15
16 public function __construct($api_key = null, $api_secret = null) {
17 $this->api_key = $api_key;
18 $this->api_secret = $api_secret;
19 $this->useServerTime();
20 }
21
22 public function balances($priceData = false) {
23 if (is_array($priceData) == false)
24 $priceData = false;
25 return $this->balanceData($this->httpRequest("/api/v1/account", "GET", array(), true), $priceData);////1
26 }
27
28 private function httpRequest($url, $method = "GET", $params = array(), $signed = false) {
29 if (function_exists('curl_init') == false) {
30 throw new \Exception("Sorry cURL is not installed!");
31 }
32
33 $ch = curl_init();
34 curl_setopt($ch, CURLOPT_VERBOSE, $this->httpDebug);
35 $query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
36
37 if ($signed == true) {
38 if (empty($this->api_key))
39 throw new \Exception("signedRequest error: API Key not set!");
40 if (empty($this->api_secret))
41 throw new \Exception("signedRequest error: API Secret not set!");
42 $base = $this->base;
43 $ts = ( microtime(true) * 1000 ) + $this->info['timeOffset'];
44 $params['timestamp'] = number_format($ts, 0, '.', '');
45 $query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
46 $signature = hash_hmac('sha256', $query, $this->api_secret);
47 $endpoint = $base . $url . '?' . $query . '&signature=' . $signature;
48 curl_setopt($ch, CURLOPT_URL, $endpoint);
49 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
50 'X-MBX-APIKEY: ' . $this->api_key
51 ));
52 }
53 else if (count($params) > 0) {
54 curl_setopt($ch, CURLOPT_URL, $this->base . $url . '?' . $query);
55 }
56 else {
57 curl_setopt($ch, CURLOPT_URL, $this->base . $url);
58 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
59 'X-MBX-APIKEY: ' . $this->api_key
60 ));
61 }
62 curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/4.0 (compatible; PHP Binance API)");
63 if ($method == "POST") {
64 curl_setopt($ch, CURLOPT_POST, true);
65 }
66 if ($method == "DELETE") {
67 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
68 }
69
70 //echo "<br>endpoint=".$endpoint." query=".$query;
71 //var_dump($params);
72
73 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
74 curl_setopt($ch, CURLOPT_HEADER, false);
75 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
76 curl_setopt($ch, CURLOPT_TIMEOUT, 60);
77 $output = curl_exec($ch);
78 if (curl_errno($ch) > 0) {
79 echo 'Curl error: ' . curl_error($ch) . "\n";
80 return array();
81 }
82 curl_close($ch);
83 $json = json_decode($output, true);
84 if (isset($json['msg'])) {
85 echo "signedRequest error: {$output}" . PHP_EOL;
86 }
87 return $json;
88 }
89
90
91 public function order($symbol, $side, $type = "LIMIT", $quantity, $leverage, $takeprofit, $price, $flags = array(), $test = false) {
92 $opt = array(
93"symbol" => $symbol,
94"side" => $side,
95"type" => $type,
96"quantity" => $quantity,
97"accountId" => 12414598682060098,
98"recvWindow" => 60000,
99"timestamp" => ( microtime(true) * 1000 )
100);
101
102
103
104 if (gettype($price) != "string") {
105 $price = number_format($price, 5, '.', '');
106 }
107
108 if (is_numeric($quantity) == false) {
109 echo "warning: quantity expected numeric got " . gettype($quantity) . PHP_EOL;
110 }
111
112 if (is_string($price) == false) {
113 echo "warning: price expected string got " . gettype($price) . PHP_EOL;
114 }
115
116 if ($type === "LIMIT" || $type === "STOP_LOSS_LIMIT" || $type === "TAKE_PROFIT_LIMIT") {
117 $opt["price"] = $price;
118 $opt["timeInForce"] = "GTC";
119 }
120
121//echo "<br>type=".$type." opt_p=".$opt["price"]." opt_t=".$opt["timeInForce"];
122
123 if (isset($leverage)) {
124 $opt['leverage'] = $leverage;
125 }
126
127 if (isset($takeprofit)) {
128 $opt['takeProfit'] = $takeprofit;
129 }
130
131 if (isset($stoploss)) {
132 $opt['stopLoss'] = $stoploss;
133 }
134
135 if (isset($flags['icebergQty'])) {
136 $opt['icebergQty'] = $flags['icebergQty'];
137 }
138
139 if (isset($flags['newOrderRespType'])) {
140 $opt['newOrderRespType'] = $flags['newOrderRespType'];
141 }
142
143var_dump ($opt);
144
145 $qstring = ( $test == false ) ? "/api/v1/order" : "/api/v1/order/test";
146 return $this->httpRequest($qstring, "POST", $opt, true);
147 }
148
149
150 private function balanceData($array, $priceData) {
151 if (is_array($priceData)) {
152 $btc_value = $btc_total = 0.00;
153 }
154 $balances = array();
155 if (empty($array) || empty($array['balances'])) {
156 echo "balanceData error: Please make sure your system time is synchronized, or pass the useServerTime option." . PHP_EOL;
157 return array();
158 }
159 foreach ($array['balances'] as $obj) {
160 $asset = $obj['asset'];
161 $balances[$asset] = array(
162 "available" => $obj['free'],
163 "onOrder" => $obj['locked'],
164 "btcValue" => 0.00000000,
165 "btcTotal" => 0.00000000
166 );
167 if ($priceData) {
168 if ($obj['free'] + $obj['locked'] < 0.00000001)
169 continue;
170 if ($asset == 'BTC') {
171 $balances[$asset]['btcValue'] = $obj['free'];
172 $balances[$asset]['btcTotal'] = $obj['free'] + $obj['locked'];
173 $btc_value += $obj['free'];
174 $btc_total += $obj['free'] + $obj['locked'];
175 continue;
176 }
177 $symbol = $asset . 'BTC';
178 if ($symbol == 'USDTBTC') {
179 $btcValue = number_format($obj['free'] / $priceData['BTCUSDT'], 8, '.', '');
180 $btcTotal = number_format(( $obj['free'] + $obj['locked'] ) / $priceData['BTCUSDT'], 8, '.', '');
181 }
182 elseif (!isset($priceData[$symbol])) {
183 $btcValue = $btcTotal = 0;
184 }
185 else {
186 $btcValue = number_format($obj['free'] * $priceData[$symbol], 8, '.', '');
187 $btcTotal = number_format(( $obj['free'] + $obj['locked'] ) * $priceData[$symbol], 8, '.', '');
188 }
189 $balances[$asset]['btcValue'] = $btcValue;
190 $balances[$asset]['btcTotal'] = $btcTotal;
191 $btc_value += $btcValue;
192 $btc_total += $btcTotal;
193 }
194 }
195 if (is_array($priceData)) {
196 uasort($balances, function ( $a, $b ) {
197 return $a['btcValue'] < $b['btcValue'];
198 });
199 $this->btc_value = $btc_value;
200 $this->btc_total = $btc_total;
201 }
202 return $balances;
203 }
204
205 public function useServerTime() {
206 $s = $this->httpRequest("/api/v1/time");
207 $serverTime = $s['serverTime'];
208 $this->info['timeOffset'] = $serverTime - ( microtime(true) * 1000 );
209 }
210
211
212
213 public function sell($symbol, $quantity, $price, $leverage, $takeprofit, $type = "LIMIT", $flags = array()) {
214 return $this->order($symbol, "SELL", $type, $quantity, $leverage, $takeprofit, $price, $flags);
215 }
216
217 public function buy($symbol, $quantity, $price, $leverage, $takeprofit, $type = "LIMIT", $flags = array()) {
218 return $this->order($symbol, "BUY", $type, $quantity, $leverage, $takeprofit, $price, $flags);
219 }
220
221
222
223 public function accoun() {
224 return $this->httpRequest("/api/v1/account", "GET", array(), true);////1
225 }
226
227
228
229public function openOrders($symbol) {
230 $params = array();
231 if( is_null( $symbol ) != true ) {
232 $params = array(
233 "symbol" => $symbol
234 );
235 }
236 return $this->httpRequest( "/api/v1/openOrders", "GET", $params, true );
237 }
238
239
240
241public function history($symbol, $limit=10, $fromTradeId=0) {
242 return $this->httpRequest( "/api/v1/myTrades", "GET", array(
243 "symbol" => $symbol,
244 "limit" => $limit,
245 "fromId" => $fromTradeId
246 ), true );
247 }
248
249
250public function lever($symbol) {
251 $params = array();
252 if( is_null( $symbol ) != true ) {
253 $params = array(
254 "symbol" => $symbol,
255 "timestamp" => ( microtime(true) * 1000 )
256 );
257 }
258 return $this->httpRequest( "/api/v1/leverageSettings", "GET", $params, true );
259 }
260
261
262public function tradingpositions() {
263 $params = array();
264 $params = array(
265
266 "timestamp" => ( microtime(true) * 1000 )
267 );
268
269 return $this->httpRequest( "/api/v1/tradingPositions", "GET", $params, true );
270 }
271
272
273public function sellmarket($symbol, $quantity, $leverage, $takeprofit, $type = "MARKET", $flags = array()) {
274 return $this->order($symbol, "SELL", $type, $quantity, $leverage, $takeprofit, 0, $flags);
275 }
276
277public function buymarket($symbol, $quantity, $leverage, $takeprofit, $type = "MARKET", $flags = array()) {
278 return $this->order($symbol, "BUY", $type, $quantity, $leverage, $takeprofit, 0, $flags);
279 }
280
281
282public function mytrades($symbol) {
283 $params = array();
284 if( is_null( $symbol ) != true ) {
285 $params = array(
286 "symbol" => $symbol,
287 "timestamp" => ( microtime(true) * 1000 )
288 );
289 }
290 return $this->httpRequest( "/api/v1/myTrades", "GET", $params, true );
291 }
292
293
294
295public function corpos($order_id, $takeprofit) {
296 $params = array();
297 $params = array(
298 "positionId" => $order_id,
299 "takeProfit" => $takeprofit,
300 "recvWindow" => 60000,
301 "timestamp" => ( microtime(true) * 1000 )
302 );
303
304 return $this->httpRequest( "/api/v1/updateTradingPosition", "POST", $params, true );
305 }
306
307
308}