我正在尝试使用bitfinex API。
我在PhP编程,所有文档都是JS或Ruby(我真的需要学习更多Ruby)。
我可以提取用户信息,但我无法提取订单
代码:
<?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订单?
答案 0 :(得分:2)
看起来你没有达到正确的终点。
订单也是公开的。您的示例适用于经过身份验证的端点,例如进行交易。对于公共端点,您不需要SHA384或base64任何内容,您可以只执行简单的curl
甚至file_get_contents
。
这是订单簿的端点:https://api.bitfinex.com/v1/book/BTCUSD
现在你可以从那里做你喜欢的事。
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);
这只会抛弃一切。
或者,您可以使用foreach
循环将所有要求和出价解析为表格。以下是file_get_contents
的示例,您当然可以使用cURL
执行此操作。
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>";