├── LICENSE ├── README.md ├── demo.php └── lib.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Huobi-API 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 火币REST API请求库 2 | ## 用法参考demo.php 3 | ## lib.php已经定义了一些基本方法, 如果要添加新方法,按照火币文档加入就行 https://github.com/huobiapi/API_Docs/wiki/REST_api_reference 4 | ``` 5 | // 添加新方法需要定义的参数 6 | 7 | // REST接口地址 8 | $this->api_method = "/v1/..."; 9 | // REST请求方法 一般为GET或POST 10 | $this->req_method = 'GET'; 11 | // 请求参数 根据文档要求定义 12 | $postdata = [ 13 | 'parama' => '123' 14 | ]; 15 | // 参数配置完毕后调用create_sign_url方法生成验签URL 然后curl方法发起请求并获取返回值 16 | $url = $this->create_sign_url(); 17 | $return = $this->curl($url,$postdata); 18 | ``` -------------------------------------------------------------------------------- /demo.php: -------------------------------------------------------------------------------- 1 | get_account_accounts()); 13 | // 获取账户余额示例 14 | //var_dump($req->get_balance()); 15 | 16 | ?> 17 | -------------------------------------------------------------------------------- /lib.php: -------------------------------------------------------------------------------- 1 | api = parse_url($this->url)['host']; 13 | date_default_timezone_set("Etc/GMT+0"); 14 | } 15 | /** 16 | * 行情类API 17 | */ 18 | // 获取K线数据 19 | function get_history_kline($symbol = '', $period='',$size=0) { 20 | $this->api_method = "/market/history/kline"; 21 | $this->req_method = 'GET'; 22 | $param = [ 23 | 'symbol' => $symbol, 24 | 'period' => $period 25 | ]; 26 | if ($size) $param['size'] = $size; 27 | $url = $this->create_sign_url($param); 28 | return json_decode($this->curl($url)); 29 | } 30 | // 获取聚合行情(Ticker) 31 | function get_detail_merged($symbol = '') { 32 | $this->api_method = "/market/detail/merged"; 33 | $this->req_method = 'GET'; 34 | $param = [ 35 | 'symbol' => $symbol, 36 | ]; 37 | $url = $this->create_sign_url($param); 38 | return json_decode($this->curl($url)); 39 | } 40 | // 获取 Market Depth 数据 41 | function get_market_depth($symbol = '', $type = '') { 42 | $this->api_method = "/market/depth"; 43 | $this->req_method = 'GET'; 44 | $param = [ 45 | 'symbol' => $symbol, 46 | 'type' => $type 47 | ]; 48 | $url = $this->create_sign_url($param); 49 | return json_decode($this->curl($url)); 50 | } 51 | // 获取 Trade Detail 数据 52 | function get_market_trade($symbol = '') { 53 | $this->api_method = "/market/trade"; 54 | $this->req_method = 'GET'; 55 | $param = [ 56 | 'symbol' => $symbol 57 | ]; 58 | $url = $this->create_sign_url($param); 59 | return json_decode($this->curl($url)); 60 | } 61 | // 批量获取最近的交易记录 62 | function get_history_trade($symbol = '', $size = '') { 63 | $this->api_method = "/market/history/trade"; 64 | $this->req_method = 'GET'; 65 | $param = [ 66 | 'symbol' => $symbol 67 | ]; 68 | if ($size) $param['size'] = $size; 69 | $url = $this->create_sign_url($param); 70 | return json_decode($this->curl($url)); 71 | } 72 | // 获取 Market Detail 24小时成交量数据 73 | function get_market_detail($symbol = '') { 74 | $this->api_method = "/market/detail"; 75 | $this->req_method = 'GET'; 76 | $param = [ 77 | 'symbol' => $symbol 78 | ]; 79 | $url = $this->create_sign_url($param); 80 | return json_decode($this->curl($url)); 81 | } 82 | /** 83 | * 公共类API 84 | */ 85 | // 查询系统支持的所有交易对及精度 86 | function get_common_symbols() { 87 | $this->api_method = '/v1/common/symbols'; 88 | $this->req_method = 'GET'; 89 | $url = $this->create_sign_url([]); 90 | return json_decode($this->curl($url)); 91 | } 92 | // 查询系统支持的所有币种 93 | function get_common_currencys() { 94 | $this->api_method = "/v1/common/currencys"; 95 | $this->req_method = 'GET'; 96 | $url = $this->create_sign_url([]); 97 | return json_decode($this->curl($url)); 98 | } 99 | // 查询系统当前时间 100 | function get_common_timestamp() { 101 | $this->api_method = "/v1/common/timestamp"; 102 | $this->req_method = 'GET'; 103 | $url = $this->create_sign_url([]); 104 | return json_decode($this->curl($url)); 105 | } 106 | // 查询当前用户的所有账户(即account-id) 107 | function get_account_accounts() { 108 | $this->api_method = "/v1/account/accounts"; 109 | $this->req_method = 'GET'; 110 | $url = $this->create_sign_url([]); 111 | return json_decode($this->curl($url)); 112 | } 113 | // 查询指定账户的余额 114 | function get_account_balance() { 115 | $this->api_method = "/v1/account/accounts/".ACCOUNT_ID."/balance"; 116 | $this->req_method = 'GET'; 117 | $url = $this->create_sign_url([]); 118 | return json_decode($this->curl($url)); 119 | } 120 | /** 121 | * 交易类API 122 | */ 123 | // 下单 124 | function place_order($account_id=0,$amount=0,$price=0,$symbol='',$type='') { 125 | $source = 'api'; 126 | $this->api_method = "/v1/order/orders/place"; 127 | $this->req_method = 'POST'; 128 | // 数据参数 129 | $postdata = [ 130 | 'account-id' => $account_id, 131 | 'amount' => $amount, 132 | 'source' => $source, 133 | 'symbol' => $symbol, 134 | 'type' => $type 135 | ]; 136 | if ($price) { 137 | $postdata['price'] = $price; 138 | } 139 | $url = $this->create_sign_url(); 140 | $return = $this->curl($url,$postdata); 141 | return json_decode($return); 142 | } 143 | // 申请撤销一个订单请求 144 | function cancel_order($order_id) { 145 | $source = 'api'; 146 | $this->api_method = '/v1/order/orders/'.$order_id.'/submitcancel'; 147 | $this->req_method = 'POST'; 148 | $postdata = []; 149 | $url = $this->create_sign_url(); 150 | $return = $this->curl($url,$postdata); 151 | return json_decode($return); 152 | } 153 | // 批量撤销订单 154 | function cancel_orders($order_ids = []) { 155 | $source = 'api'; 156 | $this->api_method = '/v1/order/orders/batchcancel'; 157 | $this->req_method = 'POST'; 158 | $postdata = [ 159 | 'order-ids' => $order_ids 160 | ]; 161 | $url = $this->create_sign_url(); 162 | $return = $this->curl($url,$postdata); 163 | return json_decode($return); 164 | } 165 | // 查询某个订单详情 166 | function get_order($order_id) { 167 | $this->api_method = '/v1/order/orders/'.$order_id; 168 | $this->req_method = 'GET'; 169 | $url = $this->create_sign_url(); 170 | $return = $this->curl($url); 171 | return json_decode($return); 172 | } 173 | // 查询某个订单的成交明细 174 | function get_order_matchresults($order_id = 0) { 175 | $this->api_method = '/v1/order/orders/'.$order_id.'/matchresults'; 176 | $this->req_method = 'GET'; 177 | $url = $this->create_sign_url(); 178 | $return = $this->curl($url,$postdata); 179 | return json_decode($return); 180 | } 181 | // 查询当前委托、历史委托 182 | function get_order_orders($symbol = '', $types = '',$start_date = '',$end_date = '',$states = '',$from = '',$direct='',$size = '') { 183 | $this->api_method = '/v1/order/orders'; 184 | $this->req_method = 'GET'; 185 | $postdata = [ 186 | 'symbol' => $symbol, 187 | 'states' => $states 188 | ]; 189 | if ($types) $postdata['types'] = $types; 190 | if ($start_date) $postdata['start-date'] = $start_date; 191 | if ($end_date) $postdata['end-date'] = $end_date; 192 | if ($from) $postdata['from'] = $from; 193 | if ($direct) $postdata['direct'] = $direct; 194 | if ($size) $postdata['size'] = $size; 195 | $url = $this->create_sign_url($postdata); 196 | $return = $this->curl($url); 197 | return json_decode($return); 198 | } 199 | // 查询当前成交、历史成交 200 | function get_orders_matchresults($symbol = '', $types = '',$start_date = '',$end_date = '',$from = '',$direct='',$size = '') { 201 | $this->api_method = '/v1/order/matchresults'; 202 | $this->req_method = 'GET'; 203 | $postdata = [ 204 | 'symbol' => $symbol 205 | ]; 206 | if ($types) $postdata['types'] = $types; 207 | if ($start_date) $postdata['start-date'] = $start_date; 208 | if ($end_date) $postdata['end-date'] = $end_date; 209 | if ($from) $postdata['from'] = $from; 210 | if ($direct) $postdata['direct'] = $direct; 211 | if ($size) $postdata['size'] = $size; 212 | $url = $this->create_sign_url(); 213 | $return = $this->curl($url,$postdata); 214 | return json_decode($return); 215 | } 216 | // 获取账户余额 217 | function get_balance($account_id=ACCOUNT_ID) { 218 | $this->api_method = "/v1/account/accounts/{$account_id}/balance"; 219 | $this->req_method = 'GET'; 220 | $url = $this->create_sign_url(); 221 | $return = $this->curl($url); 222 | $result = json_decode($return); 223 | return $result; 224 | } 225 | /** 226 | * 借贷类API 227 | */ 228 | // 现货账户划入至借贷账户 229 | function dw_transfer_in($symbol = '',$currency='',$amount='') { 230 | $this->api_method = "/v1/dw/transfer-in/margin"; 231 | $this->req_method = 'POST'; 232 | $postdata = [ 233 | 'symbol ' => $symbol, 234 | 'currency' => $currency, 235 | 'amount' => $amount 236 | ]; 237 | $url = $this->create_sign_url($postdata); 238 | $return = $this->curl($url); 239 | $result = json_decode($return); 240 | return $result; 241 | } 242 | // 借贷账户划出至现货账户 243 | function dw_transfer_out($symbol = '',$currency='',$amount='') { 244 | $this->api_method = "/v1/dw/transfer-out/margin"; 245 | $this->req_method = 'POST'; 246 | $postdata = [ 247 | 'symbol ' => $symbol, 248 | 'currency' => $currency, 249 | 'amount' => $amount 250 | ]; 251 | $url = $this->create_sign_url($postdata); 252 | $return = $this->curl($url); 253 | $result = json_decode($return); 254 | return $result; 255 | } 256 | // 申请借贷 257 | function margin_orders($symbol = '',$currency='',$amount='') { 258 | $this->api_method = "/v1/margin/orders"; 259 | $this->req_method = 'POST'; 260 | $postdata = [ 261 | 'symbol ' => $symbol, 262 | 'currency' => $currency, 263 | 'amount' => $amount 264 | ]; 265 | $url = $this->create_sign_url($postdata); 266 | $return = $this->curl($url); 267 | $result = json_decode($return); 268 | return $result; 269 | } 270 | // 归还借贷 271 | function repay_margin_orders($order_id='',$amount='') { 272 | $this->api_method = "/v1/margin/orders/{$order_id}/repay"; 273 | $this->req_method = 'POST'; 274 | $postdata = [ 275 | 'amount' => $amount 276 | ]; 277 | $url = $this->create_sign_url($postdata); 278 | $return = $this->curl($url); 279 | $result = json_decode($return); 280 | return $result; 281 | } 282 | // 借贷订单 283 | function get_loan_orders($symbol='',$currency='',$start_date,$end_date,$states,$from,$direct,$size) { 284 | $this->api_method = "/v1/margin/loan-orders"; 285 | $this->req_method = 'GET'; 286 | $postdata = [ 287 | 'symbol' => $symbol, 288 | 'currency' => $currency, 289 | 'states' => $states 290 | ]; 291 | if ($currency) $postdata['currency'] = $currency; 292 | if ($start_date) $postdata['start-date'] = $start_date; 293 | if ($end_date) $postdata['end-date'] = $end_date; 294 | if ($from) $postdata['from'] = $from; 295 | if ($direct) $postdata['direct'] = $direct; 296 | if ($size) $postdata['size'] = $size; 297 | $url = $this->create_sign_url($postdata); 298 | $return = $this->curl($url); 299 | $result = json_decode($return); 300 | return $result; 301 | } 302 | // 借贷账户详情 303 | function margin_balance($symbol='') { 304 | $this->api_method = "/v1/margin/accounts/balance"; 305 | $this->req_method = 'POST'; 306 | $postdata = [ 307 | ]; 308 | if ($symbol) $postdata['symbol'] = $symbol; 309 | $url = $this->create_sign_url($postdata); 310 | $return = $this->curl($url); 311 | $result = json_decode($return); 312 | return $result; 313 | } 314 | /** 315 | * 虚拟币提现API 316 | */ 317 | // 申请提现虚拟币 318 | function withdraw_create($address='',$amount='',$currency='',$fee='',$addr_tag='') { 319 | $this->api_method = "/v1/dw/withdraw/api/create"; 320 | $this->req_method = 'POST'; 321 | $postdata = [ 322 | 'address' => $address, 323 | 'amount' => $amount, 324 | 'currency' => $currency 325 | ]; 326 | if ($fee) $postdata['fee'] = $fee; 327 | if ($addr_tag) $postdata['addr-tag'] = $addr_tag; 328 | $url = $this->create_sign_url($postdata); 329 | $return = $this->curl($url); 330 | $result = json_decode($return); 331 | return $result; 332 | } 333 | // 申请取消提现虚拟币 334 | function withdraw_cancel($withdraw_id='') { 335 | $this->api_method = "/v1/dw/withdraw-virtual/{$withdraw_id}/cancel"; 336 | $this->req_method = 'POST'; 337 | $url = $this->create_sign_url(); 338 | $return = $this->curl($url); 339 | $result = json_decode($return); 340 | return $result; 341 | } 342 | /** 343 | * 类库方法 344 | */ 345 | // 生成验签URL 346 | function create_sign_url($append_param = []) { 347 | // 验签参数 348 | $param = [ 349 | 'AccessKeyId' => ACCESS_KEY, 350 | 'SignatureMethod' => 'HmacSHA256', 351 | 'SignatureVersion' => 2, 352 | 'Timestamp' => date('Y-m-d\TH:i:s', time()) 353 | ]; 354 | if ($append_param) { 355 | foreach($append_param as $k=>$ap) { 356 | $param[$k] = $ap; 357 | } 358 | } 359 | return $this->url.$this->api_method.'?'.$this->bind_param($param); 360 | } 361 | // 组合参数 362 | function bind_param($param) { 363 | $u = []; 364 | $sort_rank = []; 365 | foreach($param as $k=>$v) { 366 | $u[] = $k."=".urlencode($v); 367 | $sort_rank[] = ord($k); 368 | } 369 | asort($u); 370 | $u[] = "Signature=".urlencode($this->create_sig($u)); 371 | return implode('&', $u); 372 | } 373 | // 生成签名 374 | function create_sig($param) { 375 | $sign_param_1 = $this->req_method."\n".$this->api."\n".$this->api_method."\n".implode('&', $param); 376 | $signature = hash_hmac('sha256', $sign_param_1, SECRET_KEY, true); 377 | return base64_encode($signature); 378 | } 379 | function curl($url,$postdata=[]) { 380 | $ch = curl_init(); 381 | curl_setopt($ch,CURLOPT_URL, $url); 382 | if ($this->req_method == 'POST') { 383 | curl_setopt($ch, CURLOPT_POST, 1); 384 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata)); 385 | } 386 | curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 387 | curl_setopt($ch,CURLOPT_HEADER,0); 388 | curl_setopt($ch, CURLOPT_TIMEOUT,60); 389 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 390 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 391 | curl_setopt ($ch, CURLOPT_HTTPHEADER, [ 392 | "Content-Type: application/json", 393 | ]); 394 | $output = curl_exec($ch); 395 | $info = curl_getinfo($ch); 396 | curl_close($ch); 397 | return $output; 398 | } 399 | } 400 | ?> 401 | --------------------------------------------------------------------------------