dooode
dooode

Reputation: 197

Unable to pull orderbook, bitfinex api

I am experimenting with the bitfinex API.

I program in PhP and all the docs are JS or Ruby (I really need to learn more Ruby).

Bitfinex API docs #orderbook

I can pull user info, but I am unable to pull the orderbook

code:

<?php

function bitfinex_query($path, array $req = Array())
{
        global $config;
        // API settings, add your Key and Secret at here

        $key = "xxxxxxxxx";
        $secret = "xxxxxxxx";

        // generate a nonce to avoid problems with 32bits systems
        $mt = explode(' ', microtime());
        $req['request'] = "/v1".$path;
        $req['nonce'] = $mt[1].substr($mt[0], 2, 6);

        // generate the POST data string
        $post_data = base64_encode(json_encode($req));

        $sign = hash_hmac('sha384', $post_data, $secret);

        // generate the extra headers
        $headers = array(
                        'X-BFX-APIKEY: '.$key,
                        'X-BFX-PAYLOAD: '.$post_data,
                        'X-BFX-SIGNATURE: '.$sign,
        );

        // curl handle (initialize if required)
        static $ch = null;
        if (is_null($ch)) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERAGENT,
                'Mozilla/4.0 (compatible; Bter PHP bot; '.php_uname('a').'; PHP/'.phpversion().')'
                );
        }

        curl_setopt($ch, CURLOPT_URL, 'https://api.bitfinex.com/v1'.$path);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        // run the query
        $res = curl_exec($ch);

        if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
        //echo $res;
        $dec = json_decode($res, true);
        if (!$dec) throw new Exception('Invalid data: '.$res);
        return $dec;
}



//this works
$api_name = '/orders';
$openorders = bitfinex_query($api_name);
var_dump($openorders);

//broken
$api_name = '/book/BTCUSD';
$orderbook = bitfinex_query($api_name);
//$orderbook = bitfinex_query($api_name, array("limit_asks"=> 1, "group"=> 0));


?>

output:

array(1) {
  [0]=>
  array(16) {
    ["id"]=>
    int(880337054)
    ["symbol"]=>
    string(6) "btcusd"
    ["exchange"]=>
    NULL
    ["price"]=>
    string(6) "759.02"
    ["avg_execution_price"]=>
    string(3) "0.0"
    ["side"]=>
    string(4) "sell"
    ["type"]=>
    string(14) "exchange limit"
    ["timestamp"]=>
    string(12) "1467544183.0"
    ["is_live"]=>
    bool(true)
    ["is_cancelled"]=>
    bool(false)
    ["is_hidden"]=>
    bool(false)
    ["oco_order"]=>
    NULL
    ["was_forced"]=>
    bool(false)
    ["original_amount"]=>
    string(4) "0.05"
    ["remaining_amount"]=>
    string(4) "0.05"
    ["executed_amount"]=>
    string(3) "0.0"
  }
}

PHP Fatal error:  Uncaught exception 'Exception' with message 'Invalid data:     <!DOCTYPE html>
<!--[if IE 8]>
<html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <meta name="description"
    content="The largest and most advanced cryptocurrencies exchange">
  <meta name="keywords"
    content="bitcoin,exchange,bitcoin exchange,litecoin,ethereum,margin,trade">
  <meta property="og:title" content="Bitfinex">
  <meta property="og:description"
    content="The largest and most advanced cryptocurrencies exchange">
  <meta property="og:image" content="https://bitfinex.com/assets/bfx-stacked.png">
  <meta name="twitter:card" content="summary">
  <meta name="twitter:site" content="@bitfinex">
  <meta name="twitter:title" content="Bitfinex">
  <meta name="twitter:description"
    content="The largest and most advanced cryptocurrencies exchange">
  <meta name="twitter:image" con in /home/bitfinex/buycoinz.php on line 49

Fatal error: Uncaught exception 'Exception' with message 'Invalid data:     <!DOCTYPE html>
<!--[if IE 8]>
<html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <meta name="description"
    content="The largest and most advanced cryptocurrencies exchange">
  <meta name="keywords"
    content="bitcoin,exchange,bitcoin exchange,litecoin,ethereum,margin,trade">
  <meta property="og:title" content="Bitfinex">
  <meta property="og:description"
    content="The largest and most advanced cryptocurrencies exchange">
  <meta property="og:image" content="https://bitfinex.com/assets/bfx-stacked.png">
  <meta name="twitter:card" content="summary">
  <meta name="twitter:site" content="@bitfinex">
  <meta name="twitter:title" content="Bitfinex">
  <meta name="twitter:description"
    content="The largest and most advanced cryptocurrencies exchange">
  <meta name="twitter:image" con in /home/bitfinex/buycoinz.php on line 49

How do I correctly access the bitfinex api orderbook?

Upvotes: 0

Views: 759

Answers (1)

m1xolyd1an
m1xolyd1an

Reputation: 535

It doesn't look like you are hitting the right endpoint. Also the orderbook is public. Your example is for authenticated endpoints, like making a trade. You don't need to SHA384 or base64 anything for the public endpoints, you can just do a simple curl or even a file_get_contents.

Here's the endpoint for the orderbook: https://api.bitfinex.com/v1/book/BTCUSD

Now you can do what you'd like with it from there.

cURL

$curl = "https://api.bitfinex.com/v1/book/BTCUSD";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $curl);
$ccc = curl_exec($ch);
print_r($ccc);

This would just dump everything. Or you can parse all the asks and bids into a table using foreach loops. Below is an example with file_get_contents, you could do this with cURL as well of course.

file_get_contents Fiddle example

$fgc = json_decode(file_get_contents("https://api.bitfinex.com/v1/book/BTCUSD"), true);
echo "<table><tr><td>Bids</td><td>Asks</td></tr>";
$bids = $fgc["bids"];
echo "<tr><td valign='top'>";
foreach($bids as $details){
    echo "$".$details["price"]." - ".$details["amount"];
    echo "<br>";
}
echo "</td><td valign='top'>";
$asks = $fgc["asks"];
foreach($asks as $askDetails){
    echo "$".$askDetails["price"]." - ".$askDetails["amount"];
    echo "<br>";
}
echo "</td></tr></table>";

Upvotes: 2

Related Questions