Skip to content

Commit

Permalink
binance: auth rest api function
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Jan 14, 2018
1 parent fda559b commit e569b57
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions web/keys.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* 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', '');
Expand Down
1 change: 1 addition & 0 deletions web/serverconfig.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
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','');
Expand Down
10 changes: 10 additions & 0 deletions web/yaamp/commands/ExchangeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public function listExchangeSettings($args)

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";
Expand Down
72 changes: 72 additions & 0 deletions web/yaamp/core/exchange/binance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions web/yaamp/defaultconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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','');
Expand Down

0 comments on commit e569b57

Please sign in to comment.