2016-07-03 8 views
1

Ich experimentiere mit der Bitfinex-API.Kann Orderbook nicht ziehen, bitfinex api

Ich programmiere in PhP und alle Dokumente sind JS oder Ruby (ich muss wirklich mehr Ruby lernen).

Bitfinex API docs #orderbook

kann ich Benutzer Info ziehen, aber ich bin nicht in der Lage das Orderbuch ziehen

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


?> 

Ausgang:

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 

Wie kann ich richtig Zugang das bitfinex api Auftragsbuch?

Antwort

1

Es sieht nicht so aus, als würden Sie den richtigen Endpunkt treffen. Auch das Auftragsbuch ist öffentlich. Ihr Beispiel ist für authentifizierte Endpunkte, wie zum Beispiel für einen Trade. Sie brauchen für die öffentlichen Endpunkte nichts SHA384 oder base64, Sie können einfach eine einfache curl oder sogar eine file_get_contents tun.

Hier ist der Endpunkt für das Orderbuch: https://api.bitfinex.com/v1/book/BTCUSD

Jetzt können Sie tun, was Sie mit ihm von dort möchten.

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

Dies würde nur alles abladen. Oder Sie können alle Anfragen und Gebote in eine Tabelle mit foreach Schleifen analysieren. Unten ist ein Beispiel mit file_get_contents, Sie könnten dies natürlich auch mit cURL tun.

file_get_contentsFiddle 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>"; 
Verwandte Themen