作者 karlet

feat:账号验证

  1 +<?php
  2 +
  3 +require_once __DIR__ . '/../trader/CmBroker.php';
  4 +require_once __DIR__ . '/../trader/struct/ApiInfo.php';
  5 +require_once __DIR__ . '/../jytools/BinanceFutures.php';
  6 +
  7 +use trader\struct\ApiInfo;
  8 +use trader\exchange\binance\ExBroker;
  9 +use function jytools\output;
  10 +
  11 +$key = "yXXG3y2u00MACmETeWCNMzEBcTYePs8B300CAHGJwlSwKoSx9FuEar41Ao2Dhmly";
  12 +$secret = "0mitGNtvd0NkEe1poaJMSSuCSiIrZFA2NaebrliDISrv8HOrB25FUVPLO1tuX319";
  13 +$apiInfo = new ApiInfo($key, $secret, "");
  14 +$broker = new ExBroker($apiInfo);
  15 +$broker->getSpotCfg();
  1 +<?php
  2 +
  3 +namespace trader\exchange\binance;
  4 +
  5 +require_once __DIR__ . '/../../struct/ApiInfo.php';
  6 +require_once __DIR__ . '/../../../jytools/func.php';
  7 +
  8 +use trader\struct\ApiInfo;
  9 +use jytools\Curl;
  10 +use function jytools\getMicrotime;
  11 +
  12 +class ApiSpot
  13 +{
  14 + private $host = 'https://api.binance.com';
  15 + private ?ApiInfo $apiInfo;
  16 + public function __construct(?ApiInfo $apiInfo)
  17 + {
  18 + $this->apiInfo = $apiInfo;
  19 + }
  20 + public function setHost($host)
  21 + {
  22 + $this->host = $host;
  23 + }
  24 +
  25 + //-----------public interface ------------
  26 + //获取所有品种资金费率
  27 + public function getPremiumIndex($params)
  28 + {
  29 + $url = "/fapi/v1/premiumIndex";
  30 + $method = "GET";
  31 + return $this->request($method, $url, $params);
  32 + }
  33 + //获取交易对信息
  34 + public function getExchangeInfo($params)
  35 + {
  36 + $url = "/fapi/v1/exchangeInfo";
  37 + $method = "GET";
  38 + return $this->request($method, $url, $params);
  39 + }
  40 + //获取k线
  41 + public function klines($params)
  42 + {
  43 + $url = "/fapi/v1/klines";
  44 + $method = "GET";
  45 + return $this->request($method, $url, $params);
  46 + }
  47 + //获取深度
  48 + public function depth($params)
  49 + {
  50 + $url = "/fapi/v1/depth";
  51 + $method = "GET";
  52 + return $this->request($method, $url, $params);
  53 + }
  54 +
  55 + //-------private interface ------------
  56 + //获取用户监听key
  57 + public function getListenKey($params)
  58 + {
  59 + $url = "/fapi/v1/listenKey";
  60 + $method = "POST";
  61 + return $this->request($method, $url, [], $this->apiInfo);
  62 + }
  63 + //设置持仓模式
  64 + public function setPositionMode($params)
  65 + {
  66 + $url = "/fapi/v1/positionSide/dual";
  67 + $method = "POST";
  68 + return $this->request($method, $url, $params, $this->apiInfo);
  69 + }
  70 + //获取账户持仓风险v2
  71 + public function getPositionRiskV2($params)
  72 + {
  73 + $url = "/fapi/v2/positionRisk";
  74 + $method = "GET";
  75 + return $this->request($method, $url, $params, $this->apiInfo);
  76 + }
  77 + //下单
  78 + public function placeOrder($params)
  79 + {
  80 + $url = "/fapi/v1/order";
  81 + $method = "POST";
  82 + return $this->request($method, $url, $params, $this->apiInfo);
  83 + }
  84 + //设置杠杆
  85 + public function setLever($params)
  86 + {
  87 + $url = "/fapi/v1/leverage";
  88 + $method = "POST";
  89 + return $this->request($method, $url, $params, $this->apiInfo);
  90 + }
  91 + //查询仓位
  92 + public function accountV3($params)
  93 + {
  94 + $url = "/fapi/v3/account";
  95 + $method = "GET";
  96 + return $this->request($method, $url, $params, $this->apiInfo);
  97 + }
  98 + //查询交易对配置
  99 + public function symbolConfig($params)
  100 + {
  101 + $url = "/fapi/v1/symbolConfig";
  102 + $method = "GET";
  103 + return $this->request($method, $url, $params, $this->apiInfo);
  104 + }
  105 +
  106 + //现货
  107 + public function account($param){
  108 + $url = "/api/v3/account";
  109 + $method = "GET";
  110 + return $this->request($method, $url, $param, $this->apiInfo);
  111 + }
  112 +
  113 + //------------------------------------
  114 +
  115 + private function createSign($secret, $param)
  116 + {
  117 + $len = count($param);
  118 + if ($len == 0) {
  119 + return $param;
  120 + }
  121 + $paramStr = http_build_query($param);
  122 + return hash_hmac('sha256', $paramStr, $secret);
  123 + }
  124 +
  125 + public function request($method, $path, $param, ?ApiInfo $apiInfo = null)
  126 + {
  127 + $url = $this->host . $path;
  128 + if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) {
  129 + return 0;
  130 + }
  131 + $header = [];
  132 + if ($apiInfo != null) {
  133 + $param['timestamp'] = getMicrotime();
  134 + $param['signature'] = $this->createSign($apiInfo->secret, $param);
  135 + $header[] = "X-MBX-APIKEY:" . $apiInfo->key;
  136 + }
  137 + $data = json_encode([]);
  138 + if (strtoupper($method) == 'POST') {
  139 + $data = Curl::httpPost($url, $param, $header);
  140 + }
  141 + if (strtoupper($method) == 'GET') {
  142 + $data = Curl::httpGet($url, $param, $header);
  143 + }
  144 + if (strtoupper($method) == 'DELETE') {
  145 + $data = Curl::httpDelete($url, $param, $header);
  146 + }
  147 + return json_decode($data, true);
  148 + }
  149 +
  150 +}
@@ -4,11 +4,13 @@ namespace trader\exchange\binance; @@ -4,11 +4,13 @@ namespace trader\exchange\binance;
4 4
5 require_once __DIR__ . '/../../struct/ApiInfo.php'; 5 require_once __DIR__ . '/../../struct/ApiInfo.php';
6 require_once __DIR__ . '/Api.php'; 6 require_once __DIR__ . '/Api.php';
  7 +require_once __DIR__ . '/ApiSpot.php';
7 require_once __DIR__ . '/../../../jytools/Websocket.php'; 8 require_once __DIR__ . '/../../../jytools/Websocket.php';
8 require_once __DIR__ . '/../../../jytools/func.php'; 9 require_once __DIR__ . '/../../../jytools/func.php';
9 10
10 use trader\struct\ApiInfo; 11 use trader\struct\ApiInfo;
11 use trader\exchange\binance\Api as BnApi; 12 use trader\exchange\binance\Api as BnApi;
  13 +use trader\exchange\binance\ApiSpot as BnSpotApi;
12 use jytools\Websocket; 14 use jytools\Websocket;
13 use Swoole\Coroutine; 15 use Swoole\Coroutine;
14 16
@@ -21,6 +23,7 @@ class ExBroker @@ -21,6 +23,7 @@ class ExBroker
21 private $path = '/stream'; 23 private $path = '/stream';
22 private ?ApiInfo $apiInfo; 24 private ?ApiInfo $apiInfo;
23 private BnApi $api; 25 private BnApi $api;
  26 + private BnSpotApi $apiSpot;
24 private ?Websocket $wsAcc; 27 private ?Websocket $wsAcc;
25 private ?Websocket $wsKline; 28 private ?Websocket $wsKline;
26 private ?Websocket $wsDepth; 29 private ?Websocket $wsDepth;
@@ -37,6 +40,7 @@ class ExBroker @@ -37,6 +40,7 @@ class ExBroker
37 { 40 {
38 $this->apiInfo = $apiInfo; 41 $this->apiInfo = $apiInfo;
39 $this->api = new BnApi($apiInfo); 42 $this->api = new BnApi($apiInfo);
  43 + $this->apiSpot = new BnSpotApi($apiInfo);
40 } 44 }
41 45
42 public function setWsHost($host) 46 public function setWsHost($host)
@@ -281,4 +285,9 @@ class ExBroker @@ -281,4 +285,9 @@ class ExBroker
281 $this->depthData['lid'] = $data['u']; 285 $this->depthData['lid'] = $data['u'];
282 $this->depthData['uts'] = $data['T']; 286 $this->depthData['uts'] = $data['T'];
283 } 287 }
  288 + public function getSpotCfg()
  289 + {
  290 + $res = $this->apiSpot->account([]);
  291 + var_dump($res);
  292 + }
284 } 293 }