使用PHP和cURL的Bitfinex API v2身份验证端点 [英] Bitfinex API v2 Authenticated Endpoints using PHP and cURL

查看:49
本文介绍了使用PHP和cURL的Bitfinex API v2身份验证端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我已经很近了,但是刚刚好.我正在尝试使用Bitfinex v2 API退还我的钱包余额,但是我一直收到无效密钥"错误.

I am sure I am so close, but just off. I am trying to return my wallet balances with the Bitfinex v2 API, but I keep getting the "invalid key" error.

看看这个问题之后,我认为我的问题可能是相关,但是用 utf8_encode 更新我的代码并不能解决问题.

After having a look at this question I think my issue might be related, but updating my code with utf8_encode didn't fix the issue.

这是我第一次使用cURL,因此我不确定是否已正确设置所有选项.

This is my first time using cURL, so I'm not very confident that I have set all of the options correctly.

在此先感谢您提供的任何帮助.

Thanks in advance for any help that's offered.

到目前为止,我的代码(您必须相信设置了 _APISECRET _APIKEY ):

My code so far (you'll have to trust that _APISECRET and _APIKEY are set):

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);

推荐答案

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

I was facing the same problem today. Here my working solution:

/**
 * 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);

祝你好运!

这篇关于使用PHP和cURL的Bitfinex API v2身份验证端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆