bigcrunch/binance-connector-php

PHP中的Binance API薄层

v0.0.5 2023-11-20 08:25 UTC

This package is auto-updated.

Last update: 2024-10-01 19:22:47 UTC


README

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

安装

composer require bigcrunch/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对所有需要签名的端点可用。默认为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