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