kleninm/binance-api

适用于PHP的Binance API

v1.0.0 2023-07-29 09:25 UTC

This package is auto-updated.

Last update: 2024-09-17 22:49:55 UTC


README

GitHub Workflow Status (with event) GitHub Workflow Status (with event) Packagist License (custom server) GitHub release (with filter)

本项目旨在帮助您创建与Binance SPOT API交互的自己的项目。

支持当前版本的钱包端点市场数据端点现货账户/交易

简介

本项目需要php版本8.2或更高版本。还需要bcmath扩展和guzzle依赖项。

安装

composer require kleninm/binance-api

快速入门

在`\BinanceApi\Binance`类中,每个原始方法的名称都由url后缀`v1`、`v2`或`v3`生成的名称组成。

例如,通过表格

所有端点和它们的参数,你可以在`\BinanceApi\Binance`类的phpdoc中看到

完整文档可以在本仓库的`documentation`文件夹中找到

查看基本主题以了解更多功能

如果你想要深入了解机制并添加更多自定义功能和扩展功能,请查看深入挖掘主题

你可以在示例和分析主题中查看更多示例,包括每个函数返回的内容

简单开始

$binance = new \BinanceApi\Binance();

$fullResult = $binance->depth('BTCUSDT', 2);

$orderbook = $fullResult['response']['data'];
查看$fullResult变量
Array
(
   [request] => Array
      (
         [url] => /api/v3/depth
         [headers] => Array
            (
            )
         [query] => Array
            (
               [symbol] => BTCUSDT
               [limit] => 2
            )
         [body] => Array
            (
            )
      )
   
   [response] => Array
      (
         [data] => Array
            (
               [lastUpdateId] => 37910427874
               [bids] => Array
                  (
                     [0] => Array
                        (
                           [price] => 30319.99000000
                           [amount] => 3.58155000
                        )
                  
                     [1] => Array
                        (
                           [price] => 30319.98000000
                           [amount] => 0.09091000
                        )
                  )
               [asks] => Array
                  (
                     [0] => Array
                        (
                           [price] => 30320.00000000
                           [amount] => 21.24342000
                        )
                     
                     [1] => Array
                        (
                           [price] => 30320.05000000
                           [amount] => 0.00170000
                        )
                  )
            )
         
         [info] => Array
            (
               [statusCode] => 200
               [reasonPhrase] => OK
               [headers] => Array
                  (
                     [Content-Type] => Array
                        (
                           [0] => application/json;charset=UTF-8
                        )
                     
                     ...
                     
                     [x-mbx-uuid] => Array
                        (
                           [0] => ad6df6c5-903b-451b-904c-5ba90eb4576d
                        )
                     
                     [x-mbx-used-weight] => Array
                        (
                           [0] => 1
                        )
                     
                     [x-mbx-used-weight-1m] => Array
                        (
                           [0] => 1
                        )
                     
                     ...
                  )
            )
      )
)

如果你要使用测试网

$binanceTestNet = new \BinanceApi\Binance(TestNet::BASE_ENDPOINT);

$fullResult = $binance->depth('BTCUSDT', 2);

设置API密钥

某些端点将需要API密钥。你可以这样设置它们

$binance->setApiKeys($apiKey, $secretKey);

处理错误

try {
    $result = $binance->depth('BTCUSDT', 2);
} catch (BinanceApi\Exception\BinanceResponseException $e) {
    // This is exception throw, when binance return error message
    // https://binance-docs.github.io/apidocs/spot/en/#error-codes
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
    // It's about Guzzle exception
    // https://docs.guzzlephp.org/en/stable/quickstart.html#exceptions
}

错误列表的完整列表可以在基本主题中查看

速率限制

IP限制

$binance->getAdditional()['limits']['IP']['api'];
Array
(
    [used] => 2 // By default maximum weight in minute is 1200
    [lastRequest] => Sat, 15 Jul 2023 14:19:01 GMT
)
$dateTime = new DateTime($binance->getAdditional()['limits']['IP']['api']['lastRequest']);

$dateTime = new DateTime($binance->getAdditional()['limits']['IP']['sapi']['lastRequest']);

你知道你使用了多少权重

Binance每分钟会根据IP重置权重(api和sapi)

Binance时间

在需要使用时间的任何地方,请使用binance microtime格式

你可以通过函数现在获取binance microtime

\BinanceApi\Docs\GeneralInfo\Signed::binanceMicrotime(); //1690113560956

更多示例

所有后续示例的共同部分

$binance = new \BinanceApi\Binance();

$binance->setApiKeys($apiKey, $secretKey);

// Filter every output. Read more about it in a Basic topic or just use it if you need only a body result from request
$binance->setOutputCallback(function ($output) {
    return $output['response']['data'];
});

交易所信息

当前的交易所交易规则和符号信息

$binance->exchangeInfo();

订单簿

所有三种方法都是相同的。使用你喜欢的任何一种

$binance->depth('BTCUSDT', 5);

$binance->orderbook('BTCUSDT', 5);

$binance->orderbookBTCUSDT(5); // "BTCUSDT" you can replace with any market: "ETHUSDT", "BTCBUSD", ...

交易

所有两种方法都是相同的。使用你喜欢的任何一种

$binance->trades('BTCUSDT', 5);

$binance->tradesETHUSDT(5); // "ETHUSDT" you can replace with any market: "BTCUSDT", "BTCBUSD", ...

Klines/蜡烛图

符号的K线/蜡烛图条

$binance->klines('BTCUSDT', '1m', limit: 50);

$startTime = (new DateTime('01 Jan 2022 00:00:00 GMT'))->getTimestamp() * 1000;
$binance->klines('BTCUSDT', '1d', $startTime);

$endTime = (new DateTime('01 Jan 2023 00:00:00 GMT'))->getTimestamp() * 1000;
$binance->klines('BTCUSDT', '1d', endTime: $endTime);
$binance->secondKlines('BTCUSDT');

$binance->minuteKlines('BTCUSDT');

$binance->threeMinuteKlines('BTCUSDT');

$binance->fiveMinuteKlines('BTCUSDT');

$binance->fifteenMinuteKlines('BTCUSDT');

$binance->thirtyMinuteKlines('BTCUSDT');

$binance->hourKlines('BTCUSDT');

$binance->twoHourKlines('BTCUSDT');

$binance->fourHourKlines('BTCUSDT');

$binance->sixHourKlines('BTCUSDT');

$binance->eightHourKlines('BTCUSDT');

$binance->twelveHourKlines('BTCUSDT');

$binance->dayKlines('BTCUSDT');

$binance->threeDayKlines('BTCUSDT');

$binance->weekKlines('BTCUSDT');

$binance->monthKlines('BTCUSDT');
$startTime = new DateTime('01 Jan 2022 00:00:00 GMT');
$binance->hourKlines('BTCUSDT', $startTime, limit: 24);

$endTime = new DateTime('01 Jan 2022 00:00:00 GMT');
$binance->hourKlines('BTCUSDT', endTime: $endTime, limit: 48);

价格

符号或符号的最新价格

$binance->tickerPrice();

$binance->tickerPrice('BTCUSDT');

限价订单

$binance->order('BTCUSDT', 'BUY', 'LIMIT', 'GTC', 0.01, price: 20000);

$binance->limitOrder('BTCUSDT', 'BUY', 0.01, price: 21000);

市价订单

$binance->order('BTCUSDT', 'BUY', 'MARKET', quantity: 0.01);

$binance->marketOrder('BTCUSDT', 'SELL', 0.01);

止损订单

$binance->order('BTCUSDT', 'SELL', 'STOP_LOSS', 'GTC', 0.01, stopPrice: 25000);

$binance->stopLossOrder('BTCUSDT', 'SELL', 0.01, stopPrice: 25000);

止盈订单

$binance->order('BTCUSDT', 'SELL', 'TAKE_PROFIT', 'GTC', 0.01, stopPrice: 100000);

$binance->takeProfitOrder('BTCUSDT', 'SELL', 0.01, stopPrice: 100000);

获取开放订单

获取符号上的所有开放订单。如果没有指定符号,请小心访问。

$binance->openOrders('BTCUSDT');

$binance->openOrders();

获取订单状态

检查订单的状态。

$binance->getOrder('BTCUSDT', 8403075);

取消订单

取消一个活跃订单。

$binance->cancelOrder('BTCUSDT', 8403075);

取消所有订单

取消符号上的所有活跃订单。

$binance->cancelOpenOrders('BTCUSDT');

账户信息(包括余额)

获取当前账户信息。

$binance->account();

账户交易列表

获取特定账户和代币的交易。

$binance->myTrades('BTCUSDT');

所有代币信息

获取用户可存款和提款的代币信息。

$binance->capitalConfigGetall();

提款

提交提款请求。

$binance->capitalWithdrawApply('USDT', network: 'TRX', address: 'TNGjavWm7sMjCA4r1YhsEYGfaZtZEkXzNf', amount: 10);

$binance->withdraw('USDT', network: 'TRX', address: 'TNGjavWm7sMjCA4r1YhsEYGfaZtZEkXzNf', amount: 10);

提款历史(支持网络)

获取提款历史。

$binance->capitalWithdrawHistory();

存款地址(支持网络)

通过网络获取存款地址。

$binance->capitalDepositAddress('USDT', 'TRX');

存款历史(支持网络)

获取存款历史。

$binance->capitalDepositHisrec();

贡献

  • 请给仓库点个星或Fork 💫
  • 创建新的问题或拉取请求 🤝

许可证

Binance PHP API客户端受MIT许可证(MIT)许可。