binance/binance-connector-php

PHP中用于Binance API的轻量层

v1.6.0 2024-05-30 03:13 UTC

This package is auto-updated.

Last update: 2024-08-30 06:28:44 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'
]);

测试网

可用的现货测试网。要在测试网上进行测试

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

RecvWindow

从Binance API,recvWindow对所有需要签名的端点可用。默认情况下,它是5000毫秒。您可以将此参数设置为小于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