SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | class poloniex {
| |
| 3 | protected $api_key; | |
| 4 | protected $api_secret; | |
| 5 | protected $base_url = "https://poloniex.com/tradingApi"; | |
| 6 | protected $public_url = "https://poloniex.com/public"; | |
| 7 | - | protected $api_version = 1; |
| 7 | + | |
| 8 | public function __construct($api_key, $api_secret) {
| |
| 9 | $this->api_key = $api_key; | |
| 10 | $this->api_secret = $api_secret; | |
| 11 | } | |
| 12 | ||
| 13 | private function query($path, array $req = array()) {
| |
| 14 | // API settings | |
| 15 | $key = $this->api_key; | |
| 16 | $secret = $this->api_secret; | |
| 17 | ||
| 18 | // generate a nonce to avoid problems with 32bit systems | |
| 19 | $mt = explode(' ', microtime());
| |
| 20 | $req['nonce'] = $mt[1].substr($mt[0], 2, 6); | |
| 21 | ||
| 22 | // generate the POST data string | |
| 23 | $post_data = http_build_query($req, '', '&'); | |
| 24 | $sign = hash_hmac('sha512', $post_data, $secret);
| |
| 25 | ||
| 26 | // generate the extra headers | |
| 27 | $headers = array( | |
| 28 | 'Key: '.$key, | |
| 29 | 'Sign: '.$sign, | |
| 30 | ); | |
| 31 | ||
| 32 | // curl handle (initialize if required) | |
| 33 | static $ch = null; | |
| 34 | if (is_null($ch)) {
| |
| 35 | $ch = curl_init(); | |
| 36 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 37 | curl_setopt($ch, CURLOPT_USERAGENT, | |
| 38 | 'Mozilla/4.0 (compatible; Poloniex PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
| |
| 39 | ); | |
| 40 | } | |
| 41 | curl_setopt($ch, CURLOPT_URL, $this->base_url.$path); | |
| 42 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
| 43 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
| 44 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
| 45 | ||
| 46 | // run the query | |
| 47 | $res = curl_exec($ch); | |
| 48 | ||
| 49 | if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
| |
| 50 | //echo $res; | |
| 51 | $dec = json_decode($res, true); | |
| 52 | if (!$dec){
| |
| 53 | //throw new Exception('Invalid data: '.$res);
| |
| 54 | return false; | |
| 55 | }else{
| |
| 56 | return $dec; | |
| 57 | } | |
| 58 | } | |
| 59 | ||
| 60 | protected function retrieveJSON($URL) {
| |
| 61 | $opts = array('http' =>
| |
| 62 | array( | |
| 63 | 'method' => 'GET', | |
| 64 | 'timeout' => 10 | |
| 65 | ) | |
| 66 | ); | |
| 67 | $context = stream_context_create($opts); | |
| 68 | $feed = file_get_contents($URL, false, $context); | |
| 69 | $json = json_decode($feed, true); | |
| 70 | return $json; | |
| 71 | } | |
| 72 | ||
| 73 | public function get_balances() {
| |
| 74 | return $this->query('',
| |
| 75 | array( | |
| 76 | 'command' => 'returnBalances' | |
| 77 | ) | |
| 78 | ); | |
| 79 | } | |
| 80 | ||
| 81 | public function get_open_orders($pair) {
| |
| 82 | // reverse pair | |
| 83 | $pair_arr = explode("_", $pair);
| |
| 84 | $pair_reverse = strtoupper($pair_arr[1]."_".$pair_arr[0]); | |
| 85 | ||
| 86 | return $this->query('',
| |
| 87 | array( | |
| 88 | 'command' => 'returnOpenOrders', | |
| 89 | 'currencyPair' => $pair_reverse | |
| 90 | ) | |
| 91 | ); | |
| 92 | } | |
| 93 | ||
| 94 | public function get_my_trade_history($pair) {
| |
| 95 | return $this->query('',
| |
| 96 | array( | |
| 97 | 'command' => 'returnTradeHistory', | |
| 98 | 'currencyPair' => $pair | |
| 99 | ) | |
| 100 | ); | |
| 101 | } | |
| 102 | ||
| 103 | public function buy($pair, $rate, $amount) {
| |
| 104 | return $this->query('',
| |
| 105 | array( | |
| 106 | 'command' => 'buy', | |
| 107 | 'currencyPair' => $pair, | |
| 108 | 'rate' => $rate, | |
| 109 | 'amount' => $amount | |
| 110 | ) | |
| 111 | ); | |
| 112 | } | |
| 113 | ||
| 114 | public function sell($pair, $rate, $amount) {
| |
| 115 | return $this->query('',
| |
| 116 | array( | |
| 117 | 'command' => 'sell', | |
| 118 | 'currencyPair' => $pair, | |
| 119 | 'rate' => $rate, | |
| 120 | 'amount' => $amount | |
| 121 | ) | |
| 122 | ); | |
| 123 | } | |
| 124 | ||
| 125 | public function cancel_order($pair, $order_number) {
| |
| 126 | return $this->query('',
| |
| 127 | array( | |
| 128 | 'command' => 'cancelOrder', | |
| 129 | 'currencyPair' => $pair, | |
| 130 | 'orderNumber' => $order_number | |
| 131 | ) | |
| 132 | ); | |
| 133 | } | |
| 134 | ||
| 135 | public function withdraw($currency, $amount, $address) {
| |
| 136 | return $this->query('',
| |
| 137 | array( | |
| 138 | 'command' => 'withdraw', | |
| 139 | 'currency' => $currency, | |
| 140 | 'amount' => $amount, | |
| 141 | 'address' => $address | |
| 142 | ) | |
| 143 | ); | |
| 144 | } | |
| 145 | ||
| 146 | public function get_total_btc_balance() {
| |
| 147 | $results = $this->get_balances(); | |
| 148 | $prices = $this->get_ticker(); | |
| 149 | - | $pair = strtolower($coin."_btc"); |
| 149 | + | |
| 150 | - | $prices = $this->get_ticker($pair); |
| 150 | + | |
| 151 | - | $tot_btc += $amount * $prices['last']; |
| 151 | + | |
| 152 | foreach($results as $coin => $amount){
| |
| 153 | - | $tot_btc += $amount; |
| 153 | + | $pair_reverse = "BTC_".strtoupper($coin); |
| 154 | ||
| 155 | if($amount > 0){
| |
| 156 | if($coin != "BTC"){
| |
| 157 | $tot_btc += $amount * $prices[$pair_reverse]; | |
| 158 | }else{
| |
| 159 | $tot_btc += $amount; | |
| 160 | } | |
| 161 | } | |
| 162 | ||
| 163 | // get open orders | |
| 164 | if($coin != "BTC"){
| |
| 165 | $pair = strtoupper($coin."_btc"); | |
| 166 | $open_orders = $this->get_open_orders($pair); | |
| 167 | foreach($open_orders as $order){
| |
| 168 | if($order['type'] == 'buy'){
| |
| 169 | $tot_btc += $order['total']; | |
| 170 | }elseif($order['type'] == 'sell'){
| |
| 171 | $tot_btc += $order['amount'] * $prices[$pair_reverse]; | |
| 172 | } | |
| 173 | } | |
| 174 | } | |
| 175 | } | |
| 176 | ||
| 177 | return $tot_btc; | |
| 178 | } | |
| 179 | ||
| 180 | public function get_ticker($pair = "all") {
| |
| 181 | $prices = $this->retrieveJSON($this->public_url.'?command=returnTicker'); | |
| 182 | if($pair == "all"){
| |
| 183 | return $prices; | |
| 184 | }else{
| |
| 185 | // reverse pair | |
| 186 | $pair_arr = explode("_", $pair);
| |
| 187 | $pair_reverse = strtoupper($pair_arr[1]."_".$pair_arr[0]); | |
| 188 | if(isset($prices[$pair_reverse])){
| |
| 189 | $result = array(); | |
| 190 | $result['last'] = $prices[$pair_reverse]; | |
| 191 | return $result; | |
| 192 | }else{
| |
| 193 | return array(); | |
| 194 | } | |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | public function get_trade_history($pair) {
| |
| 199 | $trades = $this->retrieveJSON($this->public_url.'?command=returnTradeHistory¤cyPair='.$pair); | |
| 200 | return $trades; | |
| 201 | } | |
| 202 | ||
| 203 | public function get_order_book($pair) {
| |
| 204 | $orders = $this->retrieveJSON($this->public_url.'?command=returnOrderBook¤cyPair='.$pair); | |
| 205 | return $orders; | |
| 206 | } | |
| 207 | ||
| 208 | public function get_volume() {
| |
| 209 | $volume = $this->retrieveJSON($this->public_url.'?command=return24hVolume'); | |
| 210 | return $volume; | |
| 211 | } | |
| 212 | } | |
| 213 | ?> |