2016-07-03 21 views
1

我正在試驗bitfinex API。無法拉取訂單,bitfinex api

我在PhP程序和所有的文檔是JS或Ruby(我真的需要了解更多的Ruby)。

Bitfinex API docs #orderbook

我可以拉用戶信息,但我無法去拉訂單量

代碼:

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


?> 

輸出:

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 

我如何正確地訪問bitfinex API訂單?

回答

1

它看起來並不像你正在擊中正確的端點。 訂單也是公開的。您的示例用於通過身份驗證的端點,如進行交易。你不需要公共端點的SHA384或base64任何東西,你可以做一個簡單的curl甚至file_get_contents

這裏是爲手持訂單端點:https://api.bitfinex.com/v1/book/BTCUSD

現在你可以做你會用它喜歡有什麼可以做。

捲曲

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

這將只轉儲一切。 或者您可以使用foreach循環解析所有請求和出價。以下是file_get_contents的示例,您當然也可以使用cURL來做到這一點。

的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>";