2016-07-03 71 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>";