martoch/php-martoch-binance

基于 GuzzleHttp 的 BINANCE API LIBRARY 客户端

dev-main 2022-12-02 00:50 UTC

This package is auto-updated.

Last update: 2024-09-30 01:18:23 UTC


README

PHP 库,用于使用 Binance api

安装

运行 composer require martoch/php-martoch-binance

要求

PHP 7.3 或更高版本

使用方法

示例 1

有关 Binance api 的更多信息,请参阅: https://binance-docs.github.io/apidocs/
它将是您使用此库的指南

  1. 在您将工作的项目中,要求 BinanceClient 类

use MARTOCH\binance\BinanceClient;

  1. 创建一个对象的新实例,您需要您的 Binance api 密钥和您的 Binance 秘密密钥
$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
  1. 设置正确的调用配置,您可以通过阅读 api 文档来查看正确的配置

例如,让我们看看 检查服务器时间 方法 (https://binance-docs.github.io/apidocs/spot/en/#check-server-time)

正如我们所见,正确的路径和调用类型是: GET /api/v3/time,同样,我们观察到这是一个公共方法,因此安全类型是 NONE(您可以在以下网址中查看更多有关安全类型的信息:https://binance-docs.github.io/apidocs/spot/en/#endpoint-security-type

因此,配置必须设置如下

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useNONE(); //object will use NONE as security type
$binance->useGET(); //object will use GET as call method

注意:
-您可以使用以下方法使用不同的安全类型(您可以通过阅读 binance api 文档了解您想使用的方法的安全类型)

$binance->useNONE(); //NONE
$binance->useTRADE(); //TRADE
$binance->useMARGIN(); //MARGIN
$binance->useUSER_DATA(); //USER_DATA
$binance->useUSER_STREAM(); //USER_STREAM
$binance->useMARKET_DATA(); //MARKET_DATA

-对于默认的 BinanceCliente,使用 /api/v3/ 作为主路径,但是,binance 使用不同的端点来实现其所有功能,因此您可以使用以下方法进行配置

$binance->useAPIV3(); //path: /api/v3/
$binance->useFAPI(); //path: /fapi/v1/
$binance->useFAPIV2(); //path: /fapi/v2/
$binance->useSAPI(); //path: /sapi/v1/
$binance->useDAPI(); //path: /dapi/v1/
$binance->useVAPI(); //path: /vapi/v1/
$binance->useWAPI(); //path: /wapi/v3/

示例:使用端点路径 /fapi/v1/ | NONE 作为安全类型 | GET 作为调用方法

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useFAPI();
$binance->useNONE();
$binance->useGET();
  1. 调用您想使用的方法,在这个例子中是 检查服务器时间(《GET /api/v3/time

注意:此方法不需要参数,因此您必须发送一个空数组

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useNONE();
$binance->useGET();

$response = $binance->time([]);
  1. 使用您需要的响应,它将返回一个类似这样的数组
[
  "api_response" =>[
    "serverTime" => 1654802108489
  ]
  "status" => true
]

注意
-如果一切正常,您将收到状态:true
-如果 api 响应错误,您将收到状态:false,响应如下(有关错误信息,请参阅错误部分中的 api 文档)

[
  "api_response" =>[
    "code" => -1101
    "msg" => "Too many parameters; expected '0' and received '1'."
  ]
  "status" => false
]

示例 2

在某些情况下,binance api 使用的方法路径中包含多个元素
例如,让我们看看 系统状态(系统) 方法 (https://binance-docs.github.io/apidocs/spot/en/#system-status-system)

  1. 声明对象并设置配置(《GET /sapi/v1/system/status

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useSAPI(); //object will use /sapi/v1/
$binance->useNONE(); //object will use NONE as security type
$binance->useGET(); //object will use GET as call method
  1. 调用 status 方法
    注意:此方法不需要参数,因此您必须发送一个空数组
$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useSAPI(); //object will use /sapi/v1/
$binance->useNONE(); //object will use NONE as security type
$binance->useGET(); //object will use GET as call method

$result = $binance->_system()->status([]);  

-正如我们所见,我们在调用方法的最后将调用的方法 system 连接在一起
-在最终方法之前声明的所有路径方法都必须包含一个下划线 _,在这个例子中

->_system()

-在某些情况下,路径或最终方法包含连字符,例如 创建虚拟子账户(用于主账户):
POST /sapi/v1/sub-account/virtualSubAccount

然后您需要将连字符符号替换为单词 HYPHEN 并声明为

->_subHYPHENaccount()->virtualSubAccount([]);

-在某些情况下,路径或最终方法以数字开头,例如 24小时价格变动统计:
GET /api/v3/ticker/24hr

然后您需要在开头添加单词 call

->_ticker()->call24hr([]);
  1. 按需使用响应

示例3

示例2 相同,但使用包含多个子路径的方法
例如,让我们看看 Withdraw(USER_DATA) 方法 (https://binance-docs.github.io/apidocs/spot/en/#withdraw-user_data)

  1. 声明您的对象并设置配置 (POST /sapi/v1/capital/withdraw/apply (HMAC SHA256))
$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useSAPI(); //object will use /sapi/v1/
$binance->useUSER_DATA(); //object will use USER_DATA as security type
$binance->usePOST(); //object will use POST as call method
  1. 调用 apply 方法

注意
-该路径包含 capitalwithdraw
-在此示例中,该方法需要一些参数,因此您需要声明一个包含所有所需参数的数组(有关所需参数的更多信息,请参阅您想要使用的方法的API文档)

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useSAPI(); //object will use /sapi/v1/
$binance->useUSER_DATA(); //object will use USER_DATA as security type
$binance->usePOST(); //object will use POST as call method

$params = [
  'coin'=>'BNB'
  'timestamp'=>1654732800011,
  'otherparam'=>'something here',
  'otherparam2'=>'something here',
  ....
];

$result = $binance->_capital()->_withdraw()->apply($params);
  1. 按需使用响应

附加信息

  1. Binance有一个 TESTNET,要使用此API,您需要在配置对象时调用 useTESTNET(),此方法将binance API的端点URL更改为testnet端点

https://api.binance.com -> https://testnet.binancefuture.com

注意:testnet通常使用 /fapi/v1/ 路径,但可能有所不同,记得在配置对象时设置它

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->useTESTNET();
$binance->useFAPI();

  1. Binance API包含多个端点(集群),您可以使用 urlClusterConfig($index) 方法在它们之间切换(更多信息请参阅API文档页面)

其中 $index 是以下数组的数组位置(请记住,如果您已启用testnet,则索引将是testnet集群的索引,否则它将是正常API集群的索引)

注意:当您创建BiananceClient对象时,默认的 $index 是0

正常API集群

    [
        "https://api.binance.com", // index = 0
        "https://api1.binance.com", // index = 1
        "https://api2.binance.com", // index = 2
        "https://api3.binance.com" // index = 3
    ]

Testnet API集群

    [
        "https://testnet.binancefuture.com", // index = 0
        
    ]

示例

$my_api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$my_secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$binance = new BinanceClient($my_api_key,$my_secret_key);
$binance->urlClusterConfig(1); //this will change the endpoint for: https://api1.binance.com
$binance->useFAPI();

许可证

本产品是在MIT许可证下分发的。