承接 kleninm/binance-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

kleninm/binance-api

Composer 安装命令:

composer require kleninm/binance-api

包简介

Binance API for php

README 文档

README

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

The purpose of this project is to assist you in creating your own projects that interact with the Binance SPOT API.

Supported in the current version Wallet Endpoints, Market Data Endpoints and Spot Account/Trade

Introduction

This project requires php version more or equal 8.2. Also it requires bcmath extension and guzzle dependency

Installation

composer require kleninm/binance-api

Quick start

Every original method's name in \BinanceApi\Binance class created by name from url after prefix v1, v2 or v3.

For example, by table:

Link to endpoint Method name

Order Book

$binance->depth($symbol, $limit);

Symbol Order Book Ticker

$binance->tickerBookTicker($symbol);

24hr Ticker Price Change Statistics

$binance->ticker24hr($symbol, $symbols, $type);

Withdraw History (supporting network)

$binance->capitalWithdrawHistory();

All endpoints and their methods with parameters you can see in phpdoc in \BinanceApi\Binance class

Full docs you can find in documentation folder of this repository

Look at the Basic topic to learn more features

Look at the Digging deeper topic if you want to dive deep into the mechanism and add more yourself customizations and scale functionality

You can go to the Examples and Analogs topic to see more examples, include what each function returns

Simple start

$binance = new \BinanceApi\Binance();

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

$orderbook = $fullResult['response']['data'];
View $fullResult variable
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
                        )
                     
                     ...
                  )
            )
      )
)

If you want to use testnet

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

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

Set api keys

Some endpoints will require an API Keys. You can set them like this:

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

Handle often throws errors:

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
}

Full list of errors, you can see in Basic topic

Rate limits

Limits by 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']);

You know how much weight you use

Binance reset weights by IP (api and sapi) every minute

Binance time

At any places where you need to use time, use a binance microtime format

You can get binance microtime now by function:

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

More examples

Common part for all next examples

$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'];
});

Exchange info

Current exchange trading rules and symbol information

$binance->exchangeInfo();

Order book

All three methods are identical. Use that you prefer

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

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

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

Trades

All two methods are identical. Use that you prefer

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

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

Klines/Candlestick

Kline/candlestick bars for a symbol.

$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);

Prices

Latest price for a symbol or symbols.

$binance->tickerPrice();

$binance->tickerPrice('BTCUSDT');

Limit order

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

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

Market order

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

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

Stop loss order

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

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

Take profit order

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

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

Get open orders

Get all open orders on a symbol. Careful when accessing this with no symbol.

$binance->openOrders('BTCUSDT');

$binance->openOrders();

Get order status

Check an order's status.

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

Cancel order

Cancel an active order.

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

Cancel all orders

Cancels all active orders on a symbol.

$binance->cancelOpenOrders('BTCUSDT');

Account Information (Including Balances)

Get current account information.

$binance->account();

Account Trade List

Get trades for a specific account and symbol.

$binance->myTrades('BTCUSDT');

All Coins' Information

Get information of coins (available for deposit and withdraw) for user.

$binance->capitalConfigGetall();

Withdraw

Submit a withdraw request.

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

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

Withdraw History (supporting network)

Fetch withdraw history.

$binance->capitalWithdrawHistory();

Deposit Address (supporting network)

Fetch deposit address with network.

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

Deposit History (supporting network)

Fetch deposit history.

$binance->capitalDepositHisrec();

Contributing

  • Please give a star or fork repository 💫
  • Create a new issues or pull requests 🤝

License

Binance PHP API Client is licensed under The MIT License (MIT).

kleninm/binance-api 适用场景与选型建议

kleninm/binance-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 268 次下载、GitHub Stars 达 10, 最近一次更新时间为 2023 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 kleninm/binance-api 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 268
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-29