使用PHP和cURL的Bitfinex API v2经过身份验证的端点

时间:2017-10-07 00:57:12

标签: php curl

我确信我是如此亲密,但刚刚关闭。我正在尝试使用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);

1 个答案:

答案 0 :(得分: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);
祝你好运!