【问题标题】:I cant get the bittrex v3 api in php working我无法让 php 中的 bittrex v3 api 工作
【发布时间】:2020-02-03 12:12:42
【问题描述】:

我正在尝试让 v3 api 在 PHP 中运行,你能帮我找出我做错了什么吗?

我得到的当前响应是“未经授权”,(旧的 v1.1 使用我拥有的 api 密钥和秘密)。

$时间戳=时间()* 1000; $url = "https://api.bittrex.com/v3/balances"; $方法=“获取”; $contentHash = hash('sha512', ''); $auth = $时间戳。 $网址。 $方法。 $内容哈希; $sign=hash_hmac('sha512',$auth,$apisecret); $标头=数组( 'Api-Key' => $apikey, 'Api-Timestamp' => $timestamp, 'Api-Content-Hash' => $contentHash, 'API 签名' => $sign, ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $execResult = curl_exec($ch); curl_close($ch); $obj = json_decode($execResult, TRUE);

【问题讨论】:

  • 我让它在下面正常工作,但我无法让它在没有狂暴的情况下工作。任何人都可以将下面的答案转换为上面的简单 curl_setopt 方法吗?

标签: php api curl php-curl


【解决方案1】:

这是我的工作代码:

- 获取余额:

$timestamp = time()*1000;
$url = "https://api.bittrex.com/v3/balances";
$method = "GET";
$content = "";
$subaccountId = "";
$contentHash = hash('sha512', $content);
$preSign = $timestamp . $url . $method . $contentHash . $subaccountId;
$signature = hash_hmac('sha512', $preSign, $apisecret);

$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Api-Key: ".$apikey."",
"Api-Signature: ".$signature."",
"Api-Timestamp: ".$timestamp."",
"Api-Content-Hash: ".$contentHash.""
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$execResult = curl_exec($ch);
curl_close($ch);
echo $execResult;

- 用于创建新订单:

$timestamp = time()*1000;
$url = "https://api.bittrex.com/v3/orders";
$method = "POST";

$content = '{
  "marketSymbol": "BTC-USDT",
  "direction": "BUY",
  "type": "LIMIT",
  "quantity": "0.00276225",
  "limit": "7226.00306482",
  "timeInForce": "GOOD_TIL_CANCELLED"
}';

$subaccountId = "";
$contentHash = hash('sha512', $content);
$preSign = $timestamp . $url . $method . $contentHash . $subaccountId;
$signature = hash_hmac('sha512', $preSign, $apisecret);

$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Api-Key: ".$apikey."",
"Api-Signature: ".$signature."",
"Api-Timestamp: ".$timestamp."",
"Api-Content-Hash: ".$contentHash.""
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$execResult = curl_exec($ch);
curl_close($ch);

echo $execResult;

【讨论】:

  • 请回答他的问题。他问他做错了什么。
【解决方案2】:
使用 Http\Promise\Promise; 使用 GuzzleHttp\Client; 使用 GuzzleHttp\Exception\RequestException; 使用 GuzzleHttp\HandlerStack; 使用 GuzzleRetry\GuzzleRetryMiddleware; 使用 GuzzleHttp\Psr7\Request; 使用 guzzle\guzzle; 使用独白\记录器; 包括'../vendor/autoload.php'; 函数请求时间戳() { list($usec, $sec) = explode(' ', microtime()); 返回 (int) ((int) $sec * 1000 + ((float) $usec * 1000)); } $apiKey = 'YOUR_API_KEY'; $apiSecret = 'YOUR_SECRET_KEY'; $url = 'https://api.bittrex.com/v3/balances'; $方法 = 'GET'; $内容 = ''; $timestamp = requestTimestamp(); $contentHash = hash('sha512', $content); $preSignature = ($timestamp.$url.$method.$contentHash); $signature = hash_hmac('sha512', $preSignature, $apiSecret); $headers = ['Api-Timestamp'=> $timestamp, 'Api-Key'=> $apiKey, 'Api-Content-Hash' => $contentHash, 'Api-Signature' => $signature, 'Accept'= > '应用程序/json', '内容类型'=> '应用程序/json']; $client = new \GuzzleHttp\Client(); $response = $client->request($method, $url, ['headers' => $headers], json_encode($content)); 回声 $response->getBody();

【讨论】:

    【解决方案3】:

    您可以尝试将 Content-Type 标头集添加到 application/json。

    $headers = array (
        'Api-Key' => $apikey,
        'Api-Timestamp' => $timestamp,
        'Api-Content-Hash' => $contentHash,
        'Api-Signature' => $sign,
        'Content-Type' => "application/json"
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多