linwj/huobi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

linwj/huobi

Composer 安装命令:

composer require linwj/huobi

包简介

Huobi API Like the official document interface, Support for arbitrary extension.

README 文档

README

It is recommended that you read the official document first

Huobi docs https://huobiapi.github.io/docs/spot/v1/cn/#api

All interface methods are initialized the same as those provided by huobi. See details src/api

Support Websocket

Most of the interface is now complete, and the user can continue to extend it based on my design, working with me to improve it.

中文文档

Other exchanges API

Exchanges It includes all of the following exchanges and is highly recommended.

Bitmex Support Websocket

Okex Support Websocket

Huobi Support Websocket

Binance Support Websocket

Kucoin

Mxc

Coinbase

ZB

Bitfinex

Bittrex

Kraken

Gate

Bigone

Crex24

Bybit

Coinbene

Bitget

Poloniex

Installation

composer require linwj/huobi

Support for more request Settings More

$huobi=new HuobiSpot();

//You can set special needs
$huobi->setOptions([
    //Set the request timeout to 60 seconds by default
    'timeout'=>10,
    //https://github.com/guzzle/guzzle
    'proxy'=>[],
    //https://www.php.net/manual/en/book.curl.php
    'curl'=>[],
]);

Spot Trading API

Market related API More

$huobi=new HuobiSpot();

//Get market data. This endpoint provides the snapshots of market data and can be used without verifications.
try {
    $result=$huobi->market()->getDepth([
        'symbol'=>'btcusdt',
        //'type'=>'step3'   default step0
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//List trading pairs and get the trading limit, price, and more information of different trading pairs.
try {
    $result=$huobi->market()->getTickers();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Order related API More

$huobi=new HuobiSpot($key,$secret);

//Place an Order
try {
    $result=$huobi->order()->postPlace([
        'account-id'=>$account_id,
        'symbol'=>'btcusdt',
        'type'=>'buy-limit',
        'amount'=>'0.001',
        'price'=>'100',
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}
sleep(1);

//Get order details by order ID.
try {
    $result=$huobi->order()->get([
        'order-id'=>$result['data'],
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}
sleep(1);

//Cancelling an unfilled order.
try {
    $result=$huobi->order()->postSubmitCancel([
        'order-id'=>$result['data']['id'],
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//***********************Customize the order ID
//Place an Order
try {
    $client_order_id=rand(10000,99999).rand(10000,99999);
    $result=$huobi->order()->postPlace([
        'account-id'=>$account_id,
        'symbol'=>'btcusdt',
        'type'=>'buy-limit',
        'amount'=>'0.001',
        'price'=>'1000',
        'client-order-id'=>$client_order_id,
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}
sleep(1);

//Get order details by order ID.
try {
    $result=$huobi->order()->getClientOrder([
        'clientOrderId'=>$client_order_id,
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}
sleep(1);

//Cancelling an unfilled order.
try {
    $result=$huobi->order()->postSubmitCancelClientOrder([
        'client-order-id'=>$client_order_id,
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Accounts related API More

$huobi=new HuobiSpot($key,$secret);

//get the status of an account
try {
    $result=$huobi->account()->get();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Get the balance of an account
try {
    $result=$huobi->account()->getBalance([
        'account-id'=>$result['data'][0]['id']
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

More use cases

More API

Futures Trading API

Contract related API More

$huobi=new HuobiFuture($key,$secret);

//Place an Order
try {
    $result=$huobi->contract()->postOrder([
        'symbol'=>'BTC',//string    false   "BTC","ETH"...
        'contract_type'=>'quarter',//   string  false   Contract Type ("this_week": "next_week": "quarter":)
        'contract_code'=>'BTC190628',// string  false   BTC180914
        'price'=>'100',//   decimal true    Price
        'volume'=>'1',//    long    true    Numbers of orders (amount)
        'direction'=>'buy',//   string  true    Transaction direction
        'offset'=>'open',// string  true    "open", "close"
        //'client_order_id'=>'',//long  false   Clients fill and maintain themselves, and this time must be greater than last time
        //lever_rate    int true    Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
        //order_price_type   string true    "limit", "opponent"
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Get Information of an Order
try {
    $result=$huobi->contract()->postOrderInfo([
        'order_id'=>'xxxx',//You can also 'xxxx,xxxx,xxxx' multiple ID
        //'client_order_id'=>'xxxx',
        'symbol'=>'BTC'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Cancel an Order
try {
    $result=$huobi->contract()->postCancel([
        'order_id'=>'xxxx',//You can also 'xxxx,xxxx,xxxx' multiple ID
        //'client_order_id'=>'xxxx',
        'symbol'=>'BTC'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}



//User`s position Information
try {
    $result=$huobi->contract()->postPositionInfo();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//User`s Account Information
try {
    $result=$huobi->contract()->postAccountInfo();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Get Contracts Information
try {
    $result=$huobi->contract()->getContractInfo();
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

Market related API More

$huobi=new HuobiFuture();

//The Last Trade of a Contract
try {
    $result=$huobi->market()->getTrade([
        'symbol'=>'BTC_CQ'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Request a Batch of Trade Records of a Contract
try {
    $result=$huobi->market()->getHistoryTrade([
        'symbol'=>'BTC_CQ',
        //'size'=>100
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Get Market Depth
try {
    $result=$huobi->market()->getDepth([
        'symbol'=>'BTC_CQ',
        'type'=>'step1'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

More use cases

More API

Coin Margined Swap API

$huobi=new HuobiSwap($key,$secret);
//or new
//$huobi=new HuobiLinear($key,$secret);

//Place an Order
try {
    $result=$huobi->trade()->postOrder([
        'contract_code'=>'ETH-USD',//   string  false   BTC180914
        'price'=>'100',//   decimal true    Price
        'volume'=>'1',//    long    true    Numbers of orders (amount)
        'direction'=>'buy',//   string  true    Transaction direction
        'offset'=>'open',// string  true    "open", "close"
        'order_price_type'=>'limit',//"limit", "opponent"
        'lever_rate'=>20,//int  true    Leverage rate [if“Open”is multiple orders in 10 rate, there will be not multiple orders in 20 rate
        
        //'client_order_id'=>'',//long  false   Clients fill and maintain themselves, and this time must be greater than last time
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Get Information of an Order
try {
    $result=$huobi->trade()->postOrderInfo([
        'order_id'=>$result['data']['order_id'],//You can also 'xxxx,xxxx,xxxx' multiple ID
        //'client_order_id'=>'xxxx',
        'contract_code'=>'ETH-USD'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

//Cancel an Order
try {
    $result=$huobi->trade()->postCancel([
        'order_id'=>$result['data'][0]['order_id'],//You can also 'xxxx,xxxx,xxxx' multiple ID
        //'client_order_id'=>'xxxx',
        'contract_code'=>'ETH-USD'
    ]);
    print_r($result);
}catch (\Exception $e){
    print_r($e->getMessage());
}

More Test

More Api

Websocket

Websocket has two services, server and client. The server is responsible for dealing with the new connection of the exchange, data receiving, authentication and login. Client is responsible for obtaining and processing data.Support 'Spot' and 'Futures' and 'Coin Margined' and 'Swap USDT Margined' and 'Swap Option'

Spot Websocket Demo

Server initialization must be started in Linux CLI mode.

use \Lin\Huobi\HuobiWebSocket;
require __DIR__ .'./vendor/autoload.php';

$huobi=new HuobiWebSocket();

$huobi->config([
    //Do you want to enable local logging,default false
    //'log'=>true,
    //Or set the log name
    'log'=>['filename'=>'spot'],

    //Daemons address and port,default 0.0.0.0:2211
    //'global'=>'127.0.0.1:2211',

    //Channel subscription monitoring time,2 seconds
    //'listen_time'=>2,

    //Channel data update time,default 0.5 seconds
    //'data_time'=>0.5,

    //Number of messages WS queue shuold hold, default 100
    //'queue_count'=>100,

    //Set up subscription platform, default 'spot'
    'platform'=>'spot', //options value 'spot' 'future' 'swap' 'linear' 'option'
    //Or you can set it like this
    'platform'=>[
        'type'=>'spot',//options value 'spot' 'future' 'swap' 'linear' 'option'
        'market'=>'ws://api.huobi.pro/ws',//Market Data Request and Subscription
        'order'=>'ws://api.huobi.pro/ws/v2',//Order Push Subscription

        //'market'=>'ws://api-aws.huobi.pro/ws',
        //'order'=>'ws://api-aws.huobi.pro/ws/v2',
    ],
]);

$huobi->start();

If you want to test, you can "php server.php start" immediately outputs the log at the terminal.

If you want to deploy, you can "php server.php start -d" enables resident process mode, and enables "log=>true" to view logs.

More Test

Client side initialization.

$huobi=new HuobiWebSocket();

$huobi->config([
    //Do you want to enable local logging,default false
    //'log'=>true,
    //Or set the log name
    'log'=>['filename'=>'spot'],

    //Daemons address and port,default 0.0.0.0:2211
    //'global'=>'127.0.0.1:2211',

    //Channel subscription monitoring time,2 seconds
    //'listen_time'=>2,

    //Channel data update time,default 0.5 seconds
    //'data_time'=>0.5,

    //Number of messages WS queue shuold hold, default 100
    //'queue_count'=>100,

    //Set up subscription platform, default 'spot'
    'platform'=>'spot', //options value 'spot' 'future' 'swap' 'linear' 'option'
    //Or you can set it like this
    'platform'=>[
        'type'=>'spot',//options value 'spot' 'future' 'swap' 'linear' 'option'
        'market'=>'ws://api.huobi.pro/ws',//Market Data Request and Subscription
        'order'=>'ws://api.huobi.pro/ws/v2',//Order Push Subscription

        //'market'=>'ws://api-aws.huobi.pro/ws',
        //'order'=>'ws://api-aws.huobi.pro/ws/v2',
    ],
]);

Subscribe

//You can only subscribe to public channels
$huobi->subscribe([
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',
]);

//You can also subscribe to both private and public channels.If keysecret() is set, all private channels will be subscribed by default
$huobi->keysecret([
    'key'=>'xxxxxxxxx',
    'secret'=>'xxxxxxxxx',
]);
$huobi->subscribe([
    //market
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',

    //private
    'orders#btcusdt',
    'trade.clearing#btcusdt#1',
    'accounts.update#1',
]);

Unsubscribe

//Unsubscribe from public channels
$huobi->unsubscribe([
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',
]);

//Unsubscribe from public and private channels.If keysecret() is set, all private channels will be Unsubscribed by default
$huobi->keysecret([
    'key'=>'xxxxxxxxx',
    'secret'=>'xxxxxxxxx',
]);
$huobi->unsubscribe([
    //market
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',

    //private
    'orders#btcusdt',
    'trade.clearing#btcusdt#1',
    'accounts.update#1',
]);

Get all channel subscription data

//The first way
$data=$huobi->getSubscribe();
print_r(json_encode($data));

//The second way callback
$huobi->getSubscribe(function($data){
    print_r(json_encode($data));
});

//The third way is to guard the process
$huobi->getSubscribe(function($data){
    print_r(json_encode($data));
},true);

Get partial channel subscription data

//The first way
$data=$huobi->getSubscribe([
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',
]);
print_r(json_encode($data));

//The second way callback
$huobi->getSubscribe([
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',
],function($data){
    print_r(json_encode($data));
});

//The third way is to guard the process
$huobi->getSubscribe([
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',
],function($data){
    print_r(json_encode($data));
},true);

Get partial private channel subscription data

//The first way
$huobi->keysecret($key_secret);
$data=$huobi->getSubscribe();//Return all data of private channel
print_r(json_encode($data));

//The second way callback
$huobi->keysecret($key_secret);
$huobi->getSubscribe([//Return data private and market 
    //market
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',

    //private
    'orders#btcusdt',
    'trade.clearing#btcusdt#1',
    'accounts.update#1',
],function($data){
    print_r(json_encode($data));
});

//The third way is to guard the process
$huobi->keysecret($key_secret);
$huobi->getSubscribe([//Resident process to get data return frequency $huobi->config['data_time']=0.5s
    //market
    'market.btcusdt.depth.step0',
    'market.bchusdt.depth.step0',

    //private
    'orders#btcusdt',
    'trade.clearing#btcusdt#1',
    'accounts.update#1',
],function($data){
    print_r(json_encode($data));
},true);

Spot Websocket More Test

Futures Websocket More Test

Coin Margined Websocket More Test

Swap USDT Margined Websocket More Test

linwj/huobi 适用场景与选型建议

linwj/huobi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.55k 次下载、GitHub Stars 达 41, 最近一次更新时间为 2019 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「binance」 「BitMEX」 「Huobi」 「OKEX」 「kucoin」 「kumex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 linwj/huobi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 linwj/huobi 我们能提供哪些服务?
定制开发 / 二次开发

基于 linwj/huobi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 13.55k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 44
  • 点击次数: 25
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 41
  • Watchers: 1
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-30