|
...
|
...
|
@@ -3,8 +3,11 @@ |
|
|
|
namespace trader\exchange\binance;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../struct/ApiInfo.php';
|
|
|
|
require_once __DIR__ . '/../../../jytools/func.php';
|
|
|
|
|
|
|
|
use trader\struct\ApiInfo;
|
|
|
|
use jytools\Curl;
|
|
|
|
use function jytools\getMicrotime;
|
|
|
|
|
|
|
|
class Api
|
|
|
|
{
|
|
...
|
...
|
@@ -14,10 +17,61 @@ class Api |
|
|
|
{
|
|
|
|
$this->apiInfo = $apiInfo;
|
|
|
|
}
|
|
|
|
public function setHost($host)
|
|
|
|
{
|
|
|
|
$this->host = $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPremiumIndex()
|
|
|
|
//-----------public interface ------------
|
|
|
|
public function getPremiumIndex($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/premiumIndex";
|
|
|
|
$method = "GET";
|
|
|
|
return $this->request($method, $url, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------private interface ------------
|
|
|
|
public function getListenKey($params)
|
|
|
|
{
|
|
|
|
$url = "/fapi/v1/listenKey";
|
|
|
|
$method = "POST";
|
|
|
|
return $this->request($method, $url, [], $this->apiInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------
|
|
|
|
|
|
|
|
private function createSign($secret, $param)
|
|
|
|
{
|
|
|
|
$len = count($param);
|
|
|
|
if ($len == 0) {
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
$paramStr = http_build_query($param);
|
|
|
|
return hash_hmac('sha256', $paramStr, $secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function request($method, $path, $param, ?ApiInfo $apiInfo = null)
|
|
|
|
{
|
|
|
|
$url = $this->host . $path;
|
|
|
|
if (!in_array(strtoupper($method), ['GET', 'POST', 'DELETE'])) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$header = [];
|
|
|
|
if ($apiInfo != null) {
|
|
|
|
$param['timestamp'] = getMicrotime();
|
|
|
|
$param['signature'] = $this->createSign($apiInfo->secret, $param);
|
|
|
|
$header[] = "X-MBX-APIKEY:" . $apiInfo->key;
|
|
|
|
}
|
|
|
|
$data = json_encode([]);
|
|
|
|
if (strtoupper($method) == 'POST') {
|
|
|
|
$data = Curl::httpPost($url, $param, $header);
|
|
|
|
}
|
|
|
|
if (strtoupper($method) == 'GET') {
|
|
|
|
$data = Curl::httpGet($url, $param, $header);
|
|
|
|
}
|
|
|
|
if (strtoupper($method) == 'DELETE') {
|
|
|
|
$data = Curl::httpDelete($url, $param, $header);
|
|
|
|
}
|
|
|
|
return json_decode($data, true);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|