<?php
function
bter_query(
$path
,
array
$req
=
array
()) {
$key
= '';
$secret
= '';
$mt
=
explode
(' ', microtime());
$req
['nonce'] =
$mt
[1].
substr
(
$mt
[0], 2, 6);
$post_data
= http_build_query(
$req
, '', '&');
$sign
= hash_hmac('sha512',
$post_data
,
$secret
);
$headers
=
array
(
'KEY: '.
$key
,
'SIGN: '.
$sign
,
);
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:
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$post_data
);
curl_setopt(
$ch
, CURLOPT_HTTPHEADER,
$headers
);
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER, FALSE);
$res
= curl_exec(
$ch
);
if
(
$res
=== false)
throw
new
Exception('Curl error: '.curl_error(
$ch
));
$dec
= json_decode(
$res
, true);
if
(!
$dec
)
throw
new
Exception('Invalid data: '.
$res
);
return
$dec
;
}
function
get_top_rate(
$pair
,
$type
='BUY') {
$rate
= 0;
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:
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER, FALSE);
$res
= curl_exec(
$ch
);
if
(
$res
=== false)
throw
new
Exception('Could not get reply: '.curl_error(
$ch
));
$dec
= json_decode(
$res
, true);
if
(!
$dec
)
throw
new
Exception('Invalid data: '.
$res
);
if
(
strtoupper
(
$type
) == 'BUY') {
$r
=
$dec
['bids'][0];
$rate
=
$r
[0];
}
else
{
$r
=
end
(
$dec
['asks']);
$rate
=
$r
[0];
}
return
$rate
;
}
try
{
var_dump(bter_query('1/
private
/getfunds'));
$pair
= 'ltc_btc';
$type
= 'buy';
$rate
= get_top_rate(
$pair
,
$type
) * 1.01;
var_dump(bter_query('1/
private
/placeorder',
array
(
'pair' =>
"$pair"
,
'type' =>
"$type"
,
'rate' =>
"$rate"
,
'amount' => '0.01',
)
)
);
var_dump(bter_query('1/
private
/cancelorder',
array
('order_id' => 125811)));
var_dump(bter_query('1/
private
/getorder',
array
('order_id' => 15088)));
var_dump(bter_query('1/
private
/orderlist'));
}
catch
(Exception
$e
) {
echo
"Error:"
.
$e
->getMessage();
}
?>