Bitfinex API v2 Authenticated Endpoints Using PHP and cURL

I'm sure that I am so close, but very close. I'm trying to get my Bitfinex v2 API wallet balances back, but I keep getting an invalid key error.

After looking at this question, I think my problem may be related, but updating my code with it utf8_encode

didn't help solve the problem.

This is my first time using cURL, so I'm not very sure if I set all the options correctly.

Thanks in advance for the help provided.

My code so far (you need to trust that both are installed _APISECRET

and _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


source to share


1 answer


I had the same problem today. Here's 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;
    }
}

      

I am calling the function with



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

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

      

Good luck!

+1


source







All Articles