binance: auth rest api function

This commit is contained in:
Tanguy Pruvot 2018-01-14 20:10:34 +01:00
parent fda559bca4
commit e569b57749
5 changed files with 85 additions and 0 deletions

View file

@ -7,6 +7,7 @@ define('YIIMP_MYSQLDUMP_PASS', '<my_mysql_password>');
/* Keys required to create/cancel orders and access your balances/deposit addresses */
define('EXCH_BITTREX_SECRET', '<my_bittrex_api_secret_key>');
define('EXCH_BITSTAMP_SECRET','');
define('EXCH_BINANCE_SECRET', '');
define('EXCH_BLEUTRADE_SECRET', '');
define('EXCH_BTER_SECRET', '');
define('EXCH_CCEX_SECRET', '');

View file

@ -53,6 +53,7 @@ define('EXCH_CEXIO_ID', '');
define('EXCH_CEXIO_KEY', '');
define('EXCH_COINMARKETS_USER', '');
define('EXCH_COINMARKETS_PIN', '');
define('EXCH_BINANCE_KEY', '');
define('EXCH_BITSTAMP_ID','');
define('EXCH_BITSTAMP_KEY','');
define('EXCH_HITBTC_KEY','');

View file

@ -122,6 +122,16 @@ class ExchangeCommand extends CConsoleCommand
public function testApiKeys()
{
if (!empty(EXCH_BINANCE_KEY)) {
$balance = binance_api_user('account');
if (!is_object($balance)) echo "binance error ".json_encode($balance)."\n";
else {
$assets = $balance->balances;
foreach ($assets as $asset) {
if ($asset->asset == 'BTC') echo("binance: ".json_encode($asset)."\n");
}
}
}
if (!empty(EXCH_BITSTAMP_KEY)) {
$balance = bitstamp_api_user('balance');
if (!is_array($balance)) echo "bitstamp error ".json_encode($balance)."\n";

View file

@ -4,19 +4,91 @@
function binance_api_query($method)
{
$exchange = 'binance';
$uri = "https://www.binance.com/api/v1/$method";
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Binance API PHP client; '.php_uname('s').'; PHP/'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.')');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = curl_exec($ch);
$obj = json_decode($res);
if(!is_object($obj) && !is_array($obj)) {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
debuglog("$exchange: $method failed ($status) ".strip_data($res));
}
return $obj;
}
// https://api.binance.com/api/v3/account
function binance_api_user($method, $params=NULL)
{
$exchange = 'binance';
require_once('/etc/yiimp/keys.php');
if (!defined('EXCH_BINANCE_SECRET')) define('EXCH_BINANCE_SECRET', '');
if (empty(EXCH_BINANCE_KEY) || empty(EXCH_BINANCE_SECRET)) return false;
$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 3);
$url = "https://api.binance.com/api/v3/$method";
if (empty($params)) $params = array();
$params['timestamp'] = $nonce;
$query = http_build_query($params, '', '&');
$hmac = strtolower(hash_hmac('sha256', $query, EXCH_BINANCE_SECRET));
$isPostMethod = ($method == 'order');
if ($isPostMethod)
$query .= '&signature='.$hmac;
else
$url .= '?'.$query.'&signature='.$hmac;
$headers = array(
'Content-Type: application/json;charset=UTF-8',
'X-MBX-APIKEY: '.EXCH_BINANCE_KEY,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($isPostMethod) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Binance API PHP client; '.php_uname('s').'; PHP/'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.')');
curl_setopt($ch, CURLOPT_ENCODING , '');
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_VERBOSE, 1);
$res = curl_exec($ch);
if($res === false) {
$e = curl_error($ch);
debuglog("$exchange: $method $e");
curl_close($ch);
return false;
}
$result = json_decode($res);
if(!is_object($result) && !is_array($result)) {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
debuglog("$exchange: $method failed ($status) ".strip_data($res));
}
curl_close($ch);
return $result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// manual update of one market

View file

@ -26,6 +26,7 @@ if (!defined('YAAMP_PAYMENTS_MINI')) define('YAAMP_PAYMENTS_MINI', 0.001);
if (!defined('YAAMP_ALLOW_EXCHANGE')) define('YAAMP_ALLOW_EXCHANGE', false);
if (!defined('EXCH_AUTO_WITHDRAW')) define('EXCH_AUTO_WITHDRAW', 9999.9999);
if (!defined('EXCH_BINANCE_KEY')) define('EXCH_BINANCE_KEY', '');
if (!defined('EXCH_BITTREX_KEY')) define('EXCH_BITTREX_KEY', '');
if (!defined('EXCH_BITSTAMP_ID')) define('EXCH_BITSTAMP_ID', '');
if (!defined('EXCH_BITSTAMP_KEY')) define('EXCH_BITSTAMP_KEY','');