quocvu88/binance-connector-php

Binance Connector 的分支 - PHP 的 Binance API 瘦层

v0.0.2 2024-09-16 16:52 UTC

This package is auto-updated.

Last update: 2024-09-16 17:03:31 UTC


README

这是一个轻量级的库,作为 Binance 公共 API 的连接器。

安装

composer require binance/binance-connector-php

如何使用

require_once 'vendor/autoload.php';

$client = new \Binance\Spot();
$response = $client->time();
echo json_encode($response);


$client = new \Binance\Spot(['key' => $key, 'secret' => $secret]);
$response = $client->account();
echo json_encode($response);

请查找 examples 文件夹以获取更多端点

RSA 签名

支持 RSA 签名。

# RSA Key(Unencrypted) Authentication
$key = ''; # api key is also required
$privateKey = 'file:///path/to/rsa/private/key.pem';

$client = new \Binance\Spot([
    'key'  => $key,
    'privateKey'  => $privateKey, # pass the key file directly
    'baseURL' => 'https://testnet.binance.vision'
]);

# RSA key(Encrypted) Authentication
$key = '';
$encryptedPrivateKey = 'file:///path/to/rsa/private/key.pem';
$privateKey = openssl_pkey_get_private($encryptedPrivateKey, 'password');

$client = new \Binance\Spot([
    'key'  => $key,
    'privateKey'  => $privateKey,
    'baseURL' => 'https://testnet.binance.vision'
]);

测试网

提供 spot 测试网。为了在测试网上进行测试

$client = new \Binance\Spot([
    'baseURL' => 'https://testnet.binance.vision'
]);

RecvWindow

从 Binance API 来看,recvWindow 对所有需要签名的端点都可用。默认为 5000ms。您可以将其设置为小于 60000 的任何值,超过此限制的数值将收到 Binance 服务器的错误。

$client = new \Binance\Spot(['key' => $key, 'secret' => $secret]);
$response = $client->getOrder('BNBUSDT', [
        'orderId'    => '11',
        'recvWindow' => 10000
    ]
);

可选参数

对于端点中的可选参数,将 API 文档中的确切字段名传递到可选参数数组中。例如

$response = $client->cancelOCOOrder('BNBUSDT',
    [
        'orderListId' => '12'
    ]
);

库级别验证必填参数,缺少必填参数将抛出 Binance\Exception\MissingArgumentException

超时

秒数超时。

$client = new \Binance\Spot(['timeout' => 0.5]);

$response = $client->time();

echo json_encode($response);

显示元信息

Binance API 服务器在每条响应的头部返回权重使用情况。这非常有用,可以识别当前的使用情况。为了显示此值,只需将 show_weight_usage=True 初始化客户端即可

$client = new \Binance\Spot(['showWeightUsage' => true]);
$response = $client->time();
echo json_encode($response);

这将返回

{"data":{"serverTime":1590579807751},"weight_usage":{"x-mbx-used-weight":["2"],"x-mbx-used-weight-1m":["2"]}}

它还可以打印出所有头部,这可能非常有帮助进行调试

$client = new \Binance\Spot(['showHeader' => true]);
$response = $client->time();
echo json_encode($response);

返回将如下所示

{"data":{"serverTime":1590579942001},"header":{"Content-Type":["application/json;charset=utf-8"],"Transfer-Encoding":["chunked"],...}}

Websocket

$client = new \Binance\Websocket\Spot();

$callbacks = [
    'message' => function($conn, $msg){
        echo $msg.PHP_EOL;
    },
    'ping' => function($conn, $msg) {
        echo "received ping from server".PHP_EOL;
    }
];

$client->aggTrade('btcusdt', $callbacks);

它可以提供自定义的 websocket 连接器。

$loop = \React\EventLoop\Factory::create();
$reactConnector = new \React\Socket\Connector($loop);
$connector = new \Ratchet\Client\Connector($loop, $reactConnector);
$client = new \Binance\Websocket\Spot(['wsConnector' => $connector]);

$callbacks = [
    'message' => function($conn, $msg){
        echo "received message".PHP_EOL;
    },
    'pong' => function($conn) {
        echo "received pong from server".PHP_EOL;
    },
    'ping' => function($conn) {
        echo "received ping from server".PHP_EOL;
    },
    'close' => function($conn) {
        echo "receive closed.".PHP_EOL;
    }
];

$client->miniTicker('btcusdt', $callbacks);

# send ping to server intervally
$loop->addPeriodicTimer(2, function () use ($client) {
    $client->ping();
    echo "ping sent ".PHP_EOL;
});

$loop->run();

监听合并流

$client->combined([
    'btcusdt@miniTicker',
    'ethusdt@miniTicker'
], $callbacks);

测试

# install the packages
composer install

vendor/bin/phpunit

限制

不支持期货和普通期权 API

  • /fapi/*
  • /dapi/*
  • /vapi/*
  • 相关的 Websocket 市场和用户数据流

贡献

欢迎贡献。如果您在这个项目中发现了错误,请打开一个问题来讨论您想进行哪些更改。如果是 API 的问题,请在中打开一个主题 Binance 开发者社区

许可证

MIT