· 6 years ago · Sep 15, 2019, 12:46 PM
1<?php
2 function api_query($ENDPOINT, array $REQ = array())
3 {
4 $API_PUBLIC_KEY = ''; // Your Public Api Key
5 $API_SECRET_KEY = ''; // Your Private Api Key
6
7 $PUBLIC_API = array('GetCurrencies','GetTicker','GetMarketHistory','GetMarketSummary','GetMarketSummaries','GetOrderBook');
8 $PRIVATE_API = array('GetBalance','GetBalances','GetOrder','GetOrders','SubmitOrder','CancelOrder','GetTradeHistory','GenerateAddress','SubmitWithdraw','GetDeposits','GetWithdrawals');
9
10 // Init curl
11 static $ch = null; $ch = curl_init();
12 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
13 curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; TradeSatoshi API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
14
15 // remove those 2 line to secure after test.
16 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
17 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
18
19 // PUBLIC or PRIVATE API
20 if (in_array($ENDPOINT,$PUBLIC_API))
21 {
22 $URL = "https://www.tradesatoshi.com/api/public/".strtolower($ENDPOINT);
23 if ($REQ) $URL .= '?'.http_build_query($REQ,'','&');
24 curl_setopt($ch, CURLOPT_URL, $URL );
25 }
26 elseif (in_array($ENDPOINT,$PRIVATE_API))
27 {
28 $URL = "https://www.tradesatoshi.com/api/private/".strtolower($ENDPOINT);
29 $mt = explode(' ', microtime()); $NONCE = $mt[1].substr($mt[0], 2, 6);
30 $REQ = json_encode($REQ);
31 $SIGNATURE = $API_PUBLIC_KEY.'POST'.strtolower(urlencode($URL)).$NONCE.base64_encode($REQ);
32 $HMAC_SIGN = base64_encode(hash_hmac('sha512',$SIGNATURE,base64_decode($API_SECRET_KEY),true));
33 $HEADER = 'Basic '.$API_PUBLIC_KEY.':'.$HMAC_SIGN.':'.$NONCE;
34 $HEADERS = array("Content-Type: application/json; charset=utf-8", "Authorization: $HEADER");
35 curl_setopt($ch, CURLOPT_HTTPHEADER, $HEADERS);
36 curl_setopt($ch, CURLOPT_URL, $URL );
37 curl_setopt($ch, CURLOPT_POSTFIELDS,$REQ);
38 }
39
40 // run the query
41 $res = curl_exec($ch);
42 if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
43 return json_decode($res);
44 }
45
46
47 /* ---------- Public Functions ---------- */
48 function GetCurrencies()
49 {return api_query("GetCurrencies");}
50
51 function GetTicker($market=false)
52 {if($market===false)return (object)array("success"=>false,"message"=>'GetTicker: market name e.g. "LTC_BTC" (required)'); else return api_query("GetTicker",array('market'=>$market));}
53
54 function GetMarketHistory($market=false,$count=20)
55 {if($market===false)return (object)array("success"=>false,"message"=>'GetMarketHistory: market name e.g. "LTC_BTC" (required)'); else return api_query("GetMarketHistory",array('market'=>$market,'count'=>$count));}
56
57 function GetMarketSummary($market=false)
58 {if($market===false)return (object)array("success"=>false,"message"=>'GetMarketSummary: market name e.g. "LTC_BTC" (required)'); else return api_query("GetMarketSummary",array('Market'=>$market));}
59
60 function GetMarketSummaries()
61 {return api_query("GetMarketSummaries");}
62
63 function GetOrderBook($market=false,$type='both',$depth=20)
64 {if($market===false)return (object)array("success"=>false,"message"=>'GetOrderBook: market name e.g. "LTC_BTC" (required)'); else return api_query("GetOrderBook",array('Market'=>$market,'Type'=>$type,'Depth'=>$depth));}
65
66
67 /* ---------- Privates Functions ---------- */
68
69 /* GetBalance
70 Currency: The currency of the balance to return e.g. 'BTC' (required)
71 */
72 function GetBalance($currency=false)
73 {if($currency===false)return (object)array("success"=>false,"message"=>'GetBalance: currency name e.g. "BTC" (required)'); else return api_query("GetBalance",array('Currency'=>$currency));}
74
75 // No Params
76 function GetBalances()
77 {return api_query("GetBalances");}
78
79 /* GetOrder
80 OrderId: The order to return (required)
81 */
82 function GetOrder($orderid=false)
83 {if($orderid===false)return (object)array("success"=>false,"message"=>'GetOrder: OrderId required!'); else return api_query("GetOrder",array('OrderId'=>$orderid));}
84
85 /* GetOrders
86 Market: The market name e.g. 'LTC_BTC' (optional, default: 'all')
87 Count: The maximum count of records to return (optional, deefault: 20)
88 */
89 function GetOrders($market="all",$count=20)
90 {return api_query("GetOrders",array('Market'=>$market,'Count'=>$count));}
91
92 /* SubmitOrder
93 Market: The market name e.g. 'LTC_BTC' (required)
94 Type: The order type name e.g. 'Buy', 'Sell' (required)
95 Amount: The amount to buy/sell (required)
96 Price: The price to buy/sell for (required)
97 */
98 function SubmitOrder($market=false,$type=false,$amount=false,$price=false)
99 {if($market===false||$type===false||$amount===false||$price===false)return (object)array("success"=>false,"message"=>'SubmitOrder: Market,Type,Amount,Price are required!'); else return api_query("SubmitOrder",array('Market'=>$market,'Type'=>$type,'Amount'=>$amount,'Price'=>$price));}
100
101 /* CancelOrder
102 Type: The cancel type, options: 'Single','Market','MarketBuys','MarketSells','AllBuys','AllSells','All'(required)
103 OrderId: The order to cancel(required if cancel type 'Single')
104 Market: The order to cancel(required if cancel type 'Market','MarketBuys','MarketSells')
105 */
106 function CancelOrder($type=false,$orderid=false,$market=false)
107 {if($type===false)return (object)array("success"=>false,"message"=>'CancelOrder: Type and/or OrderId and/or Market are required!'); else return api_query("CancelOrder",array('Type'=>$type,'OrderId'=>$orderid,'Market'=>$market));}
108
109 /* GetTradeHistory
110 Market: The market name e.g. 'LTC_BTC' (optional, default: 'all')
111 Count: The maximum count of records to return (optional, default: 20)
112 */
113 function GetTradeHistory($market='all',$count=20)
114 {return api_query("GetTradeHistory",array('Market'=>$market,'Count'=>$count));}
115
116 /* GenerateAddress
117 Currency: The currency to generate address for e.g. 'BTC' (required)
118 */
119 function GenerateAddress($currency=false)
120 {if($currency===false)return (object)array("success"=>false,"message"=>'GenerateAddress: The currency to generate address for e.g. "BTC" (required)'); else return api_query("GenerateAddress",array('Currency'=>$currency));}
121
122 /* SubmitWithdraw
123 Currency: The currency name e.g. 'BTC' (required)
124 Address: The receiving address (required)
125 Amount: The amount to withdraw (required)
126 */
127 function SubmitWithdraw($currency=false,$address=false,$amount=false)
128 {if($currency===false||$address===false||$amount===false)return (object)array("success"=>false,"message"=>'SubmitWithdraw: Currency,Address,Amount are required'); else return api_query('SubmitWithdraw',array('Currency'=>$currency,'Address'=>$address,'Amount'=>$amount));}
129
130 /* GetDeposits
131 Currency: The currency name e.g. 'BTC' (optional, default: 'all')
132 Count: The maximum count of records to return (optional, default: 20)
133 */
134 function GetDeposits($currency='all',$count=20)
135 {return api_query('GetDeposits',array('Currency'=>$currency,'Count'=>$count));}
136
137 /* GetWithdrawals
138 Currency: The currency name e.g. 'BTC' (optional, default: 'all')
139 Count: The maximum count of records to return (optional, default: 20)
140 */
141 function GetWithdrawals($currency='all',$count=20)
142 {return api_query('GetWithdrawals',array('Currency'=>$currency,'Count'=>$count));}
143?>