【问题标题】:Bitfinex API v2 Authenticated Endpoints using PHP and cURL使用 PHP 和 cURL 的 Bitfinex API v2 认证端点
【发布时间】:2017-10-07 00:57:12
【问题描述】:

我确定我离得很近,但就在附近。我正在尝试使用 Bitfinex v2 API 返回我的钱包余额,但我不断收到“无效密钥”错误。

查看this question 后,我认为我的问题可能与此相关,但使用utf8_encode 更新我的代码并没有解决问题。

这是我第一次使用 cURL,所以我不太确定我是否正确设置了所有选项。

提前感谢您提供的任何帮助。

到目前为止我的代码(您必须相信 _APISECRET_APIKEY 已设置):

CONST _APIPATH = "v2/auth/r/wallets";
CONST _APIURL = "https://api.bitfinex.com/";

$nonce = strval(time()*1000);
$body = json_encode(array());
$signature = '/api/' . _APIPATH . $nonce . $body;

$signature = hash_hmac('sha384', $signature, utf8_encode(_APISECRET));

$headers = array('bfx-nonce' => $nonce, 'bfx-apikey' => utf8_encode(_APIKEY), 'bfx-signature' => $signature, 'content-type' => 'application/json');

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_URL, _APIURL . _APIPATH);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_exec($ch);

curl_close($ch);

【问题讨论】:

  • 这不是 javascript,PHP 字符串是二进制安全的,如果编码不正确,你从哪里获得这个 api 密钥?
  • API 密钥与我的帐户绑定(您可以从帐户界面中获取)。我确信我输入正确。
  • 我刚刚发现bfx-signature这个字符串必须是小写的。

标签: php curl


【解决方案1】:

我今天也遇到了同样的问题。这是我的工作解决方案:

/**
 * Bitfinex API V2 REST AUTHENTICATED ENDPOINT
 *
 * @param $method
 * @param array $request
 *
 * @return mixed
 */
private function queryPrivate($method, array $request = array())
{
    // build the POST data string
    $postData = (count($request)) ? '/' . implode("/", $request) : '';

    $nonce      = (string) number_format(round(microtime(true) * 100000), 0, ".", "");
    $path       = "/api/v2".'/auth/r/'.$method.$postData.$nonce;
    $signature  = hash_hmac("sha384", utf8_encode($path), utf8_encode($this->secret));

    $headers = array(
        "content-type: application/json",
        "content-length: ",
        "bfx-apikey: " . $this->key,
        "bfx-signature: " . $signature,
        "bfx-nonce: " . $nonce
    );

    $url = $this->url.'/auth/r/' . $method . $postData;

    curl_setopt($this->curl, CURLOPT_URL, $url);
    curl_setopt($this->curl, CURLOPT_POST, true);
    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, true);

    if (!$result=curl_exec($this->curl)) {
        return $this->curl_error($this->curl);
    } else {
        // var_dump($result);
        return $result;
    }
}

我正在调用函数

    $param = array();
    $this->queryPrivate("wallets", $param);

    $param = array('tIOTETH','hist');
    $this->queryPrivate("trades", $param);

祝你好运!

【讨论】:

  • 当有 postdata 时这是否有效?这条线似乎关闭 $url = $this->url.'/auth/r/' 。 $方法。 $postData;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-24
  • 1970-01-01
  • 2021-03-05
  • 2023-04-10
相关资源
最近更新 更多