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

问题描述:

我确定我非常接近,但刚刚关闭。我试图用Bitfinex v2 API返回我的钱包余额,但我一直收到“无效密钥”错误。Bitfinex API v2使用PHP和cURL的经过身份验证的端点

看过this question之后,我认为我的问题可能有关,但使用utf8_encode更新我的代码并未解决问题。

这是使用卷曲我的第一次,所以我不是很确信我已经正确设置所有的选项。

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

到目前为止我的代码(你必须相信_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); 
+0

这不是javascript,PHP字符串是二进制安全的,如果它的编码不正确,你从哪里得到这个API密钥? – hanshenrik

+0

API密钥与我的帐户绑定(可以从帐户界面中获取)。我相信我已经正确输入了。 – MowgliB

我今天所面临的同样的问题。在这里我的工作液:

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

祝你好运!