bitmartexchange/bitmart-php-sdk-api

BitMart Cloud API 的 PHP 客户端。

v2.0.0 2024-09-19 14:04 UTC

This package is not auto-updated.

Last update: 2024-09-19 14:17:34 UTC


README

Logo

BitMart-PHP-SDK-API

License: MIT

BitMart 交易所官方 PHP 客户端,用于 BitMart Cloud API。

特性

  • 提供交易所快速交易 API
  • 简化提现流程
  • 效率更高、速度更快、延迟更低
  • 开发和维护优先
  • 专业且响应迅速的技术支持
  • 提供 WebSocket API 调用
  • 支持的 API
    • /spot/*
    • /contract/*
    • /account/*
    • 现货 WebSocket 市场流
    • 现货用户数据流
    • 合约用户数据流
    • 合约 WebSocket 市场流
  • 测试用例和示例

安装

composer require bitmartexchange/bitmart-php-sdk-api

文档

API 文档

示例

现货公共 API 示例

<?php
use BitMart\Lib\CloudConfig;
use BitMart\Spot\APISpot;

require_once __DIR__ . '/../../../vendor/autoload.php';

$APISpot = new APISpot(new CloudConfig([
    'timeoutSecond' => 5,
]));

// Get a list of all cryptocurrencies on the platform
$response = $APISpot->getCurrencies()['response'];

// Querying aggregated tickers of a particular trading pair
$response = $APISpot->getV3Ticker("BTC_USDT")['response'];

现货/保证金交易端点

<?php
use BitMart\Lib\CloudConfig;
use BitMart\Spot\APISpot;

require_once __DIR__ . '/../../../vendor/autoload.php';

$APISpot = new APISpot(new CloudConfig([
    'accessKey' => "<your_api_key>",
    'secretKey' => "<your_secret_key>",
    'memo' => "<your_memo>",
]));


$response = $APISpot->postSubmitOrder(
    'BTC_USDT',
    'buy',
    'limit',
    [
        'size' => '0.1',
        'price' => '8800',
        'client_order_id' => 'test20000000005'
    ]
)['response'];

echo json_encode($response);


$response = $APISpot->postSubmitOrder(
    'BTC_USDT',
    'buy',
    'market',
    [
        'size' => '0.1',
        'notional' => '8800',
        'client_order_id' => 'test20000000006'
    ]
)['response'];

echo json_encode($response);

请查看 example/spot/ 文件夹以获取更多端点。

现货 WebSocket 订阅私有频道

<?php

use BitMart\Websocket\Spot\WsSpotPrv;

require_once __DIR__ . '/../../../vendor/autoload.php';

$ws = new WsSpotPrv([
    'accessKey' => "<your_api_key>",
    'secretKey' => "<your_secret_key>",
    'memo' => "<your_memo>",
    'xdebug' => false
]);

// Subscribe Private Channels
$ws->subscribe(
    [
        'op' => "subscribe",
        'args' => [
            // Only Support Private Channel
            "spot/user/order:BTC_USDT",
        ]
    ],
    function ($data) {
        echo "-------------------------" . PHP_EOL;
        print_r($data);
    }
);

现货 WebSocket 订阅公共频道

<?php
use BitMart\Websocket\Spot\WsSpotPub;

require_once __DIR__ . '/../../../vendor/autoload.php';

$ws = new WsSpotPub();

// Subscribe Public Channels
$ws->subscribe(
    [
        'op' => "subscribe",
        'args' => [
            // Only Support Public Channel
            "spot/ticker:BTC_USDT",
        ]
    ],
    function ($data) {
        echo "-------------------------" . PHP_EOL;
        echo print_r($data);
    }
);

请查看 example/spot/Websocket/ 文件夹以获取更多端点。

期货市场数据端点

<?php

use BitMart\Futures\APIContractMarket;
use BitMart\Lib\CloudConfig;

require_once __DIR__ . '/../../../vendor/autoload.php';

$APIContract = new APIContractMarket(new CloudConfig([
    'timeoutSecond' => 5,
]));

$response = $APIContract->getContractDetails("BTCUSDT")['response'];

echo json_encode($response);

期货交易端点

<?php

use BitMart\Futures\APIContractTrading;
use BitMart\Lib\CloudConfig;

require_once __DIR__ . '/../../../vendor/autoload.php';

$APIContract = new APIContractTrading(new CloudConfig([
    'accessKey' => "<your_api_key>",
    'secretKey' => "<your_secret_key>",
    'memo' => "<your_memo>",
]));

$response = $APIContract->submitOrder(
    'BTCUSDT',
    1,
    [
        'client_order_id' => "test3000000001",
        'type' => "limit",
        'leverage' => "1",
        'open_type' => "isolated",
        'mode' => 1,
        'price' => "10",
        'size' => 1,
    ]
)['response'];

echo json_encode($response);

请查看 example/futures/ 文件夹以获取更多端点。

期货 WebSocket 订阅私有频道

<?php

use BitMart\Websocket\Futures\WsContractPrv;

include_once __DIR__ . '/../../../vendor/autoload.php';


$ws = new WsContractPrv([
    'accessKey' => "<your_api_key>",
    'secretKey' => "<your_secret_key>",
    'memo' => "<your_memo>",
]);


// Subscribe Public Channels
$ws->subscribe(
    [
        'action' => "subscribe",
        'args' => [
            "futures/asset:USDT"
        ]
    ],
    function ($data) {
        echo "-------------------------" . PHP_EOL;
        echo print_r($data);
    }
);

期货 WebSocket 订阅公共频道

<?php

use BitMart\Websocket\Futures\WsContractPub;


include_once __DIR__ . '/../../../vendor/autoload.php';

$ws = new WsContractPub();


// Subscribe Public Channels
$ws->subscribe(
    [
        'action' => "subscribe",
        'args' => [
            // Only Support Public Channel
            "futures/ticker"
        ]
    ],
    function ($data) {
        echo "-------------------------" . PHP_EOL;
        echo print_r($data);
    }
);

请查看 example/futures/Websocket/ 文件夹以获取更多端点。

额外选项

身份验证

如何设置 API 密钥?

$APISpot = new APISpot(new CloudConfig(
      [
          'accessKey' => "your_api_key",
          'secretKey' => "your_secret_key",
          'memo' => "your_memo",
      ]
  ));

超时

设置 HTTP 连接超时读取超时

$APISpot = new APISpot(new CloudConfig(
      [
          'timeoutSecond' => 5
      ]
  ));

日志记录

如果您想 调试 API 请求的数据和 API 返回的相应数据,可以设置如下

$APISpot = new APISpot(new CloudConfig(
      [
          'xdebug' => true
      ]
  ));

域名

如何设置 API 域名?域名参数是可选的,默认域名是 https://api-cloud.bitmart.com

$APISpot = new APISpot(new CloudConfig(
      [
          'url' => 'https://api-cloud.bitmart.com'
      ]
  ));

自定义请求头

您可以在此处添加自己的请求头信息,但请勿填写 X-BM-KEY, X-BM-SIGN, X-BM-TIMESTAMP

$APISpot = new APISpot(new CloudConfig([
    'customHeaders' => array(
        "Your-Custom-Header1" => "value1",
        "Your-Custom-Header2" => "value2",
    ),
]));

响应元数据

bitmart API 服务器在每个响应的头部提供了端点速率限制的使用情况。此信息可以从 headers 属性中获取。x-bm-ratelimit-remaining 表示当前窗口已被使用的次数,x-bm-ratelimit-limit 表示当前窗口可以使用的最大次数,x-bm-ratelimit-reset 表示当前窗口的时间。

示例
x-bm-ratelimit-mode: IP
x-bm-ratelimit-remaining: 10
x-bm-ratelimit-limit: 600
x-bm-ratelimit-reset: 60

这意味着此 IP 在 60 秒内可以调用端点 600 次,目前已调用 10 次。

$response = $APISpot->getV3Ticker("BTC_USDT");

echo $response['limit']['Remaining'];
echo $response['limit']['Limit'];
echo $response['limit']['Reset'];
echo $response['limit']['Mode'];