我不能用php创建一个带kucoin api的订单

时间:2018-01-28 17:00:08

标签: php

您好我正在使用该代码创建购买订单,但它向我显示错误" {"代码":" UNAUTH"," msg": "签名验证失败","成功":false,"时间戳":1517154443105}"

$ku_key = 'KEY';

$ku_secret = 'SECRET';

$host = "https://api.kucoin.com";

$nonce = round(microtime(true) * 1000);

$endpoint = "/v1/order";

$querystring = "symbol=POE-BTC&price=0.00000748&amount=5514.70588235&type=BUY";

$signstring = $endpoint.'/'.$nonce.'/'.$querystring;

$hash = hash_hmac('sha256', base64_encode($signstring) , $ku_secret);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $host . $endpoint);

$headers = [ 'KC-API-SIGNATURE:' . $hash, 'KC-API-KEY:' . $ku_key, 'KC-API-NONCE:' . $nonce, 'Content-Type:application/json' ];

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Kucoin Bot; '.php_uname('a').'; PHP/'.phpversion().')' ); 

/* 

YOU CAN USE THIS SECTION, I USE BOTH OF THEM WITH THIS AND WITHOUT THIS. NOT WORKING WITH BOTH. 

curl_setopt($ch, CURLOPT_POST, 1); 

curl_setopt($ch, CURLOPT_POSTFIELDS, "symbol=POE-BTC&price=0.00000748&amount=5514.70588235&type=BUY"); 

*/ 

curl_setopt($ch, CURLOPT_URL, $host . $endpoint); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

$data = curl_exec($ch);

var_dump($data);

2 个答案:

答案 0 :(得分:1)

今天,当我在交易软件中实现KuCoin API时,我也面临着同样的错误消息。但是,我知道了。请在下面找到我的工作解决方案:

/**
 * 
 * @see https://kucoinapidocs.docs.apiary.io/#introduction/authentication/signature-calculation
 * 
 */
private function signedRequest($endpoint, $params = array())
{
    $ch = curl_init();

    // Must be synced, KuCoin rejects requests > 3s
    $nonce = round(microtime(true) * 1000);

    // Build the data string.
    // Note - According documentation:
    // Arrange the parameters in ascending alphabetical order (lower cases first)
    $queryString = http_build_query($params, '', '&');

    // Splice string for signing
    $auth = $endpoint . '/' . $nonce . '/' . $queryString;

    // Make a base64 encoding of the completed string
    $signedStr = base64_encode($auth);

    $signature = hash_hmac('sha256', $signedStr, $this->secret);

    $headers = array (
        'KC-API-KEY:' . $this->key,
        'KC-API-NONCE:' . $nonce,
        'KC-API-SIGNATURE:' . $signature,
    );

    // POST is not allowed so we attach the parameter to the url
    $curl = $this->base . $endpoint . "?" . $queryString;

    // make the request
    curl_setopt($ch, CURLOPT_URL, $curl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // do not print output to screen
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


    $result = curl_exec($ch);

    return $result;
}

经过端点'/ v1 / account / balances'测试

public function getWalletBalance()
{
    $params = array(
      'limit' => '12',
      'page' => '1'
    );

    $balance = $this->signedRequest("/v1/account/balances", $params);

    return $this->_getFormattedBalance($balance);
}

答案 1 :(得分:0)

您需要在查询字符串和发布请求中按字母顺序排序params。他们的文档需要一些改进,但在某些地方已经说明了这一点。

$querystring = "amount=5514.70588235&price=0.00000748&symbol=POE-BTC&type=BUY";

curl_setopt($ch, CURLOPT_POSTFIELDS, "amount=5514.70588235&price=0.00000748&symbol=POE-BTC&type=BUY");