Skip to content

Instantly share code, notes, and snippets.

@erezwanderman
Created March 7, 2017 21:06
Show Gist options
  • Select an option

  • Save erezwanderman/5541fdd62a7b2bc69c2f2aa29b609bb3 to your computer and use it in GitHub Desktop.

Select an option

Save erezwanderman/5541fdd62a7b2bc69c2f2aa29b609bb3 to your computer and use it in GitHub Desktop.
<?php
require_once(__DIR__ . "/global.php");
function bit2c_query($key, $secret, $path, array $req = array()) {
static $curl_tmpl = null;
if ($curl_tmpl === null) {
$curl_tmpl = curl_init();
curl_setopt($curl_tmpl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_tmpl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0');
//curl_setopt($ch, CURLOPT_URL, $path);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_tmpl, CURLOPT_SSL_VERIFYPEER, FALSE);
}
//curl_copy_handle
//// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
//$mt = explode(' ', microtime());
//$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
//$req['nonce'] = time();
$req['nonce'] = number_format(microtime(true) * 1000000, 0, '.', '');
// generate the POST data string
$post_data = http_build_query($req, '', '&');
// generate the extra headers
$headers = array(
'Content-Type: application/x-www-form-urlencoded',
'Key: '.$key,
'Sign: '.base64_encode(hash_hmac('sha512', $post_data, strtoupper($secret), true)),
);
// our curl handle (initialize if required)
//$ch = curl_init();
$ch = curl_copy_handle($curl_tmpl);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Bit2c PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_URL, $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('Could not get reply: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) {
if ($res == "You must wait 1 seconds before accessing this url again.") {
sleep(2);
$ch = curl_copy_handle($curl_tmpl);
$req['nonce'] = number_format(microtime(true) * 1000000, 0, '.', '');
$post_data = http_build_query($req, '', '&');
$headers = array(
'Content-Type: application/x-www-form-urlencoded',
'Key: '.$key,
'Sign: '.base64_encode(hash_hmac('sha512', $post_data, strtoupper($secret), true)),
);
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) {
echo "Error on second attempt: " . $res . "\n";
throw new Exception('Invalid data received');
}
}
if (!$dec) {
echo "Error: " . $res . "\n";
throw new Exception('Invalid data received');
}
}
if (isset($dec['error']) && $dec['error'] == "Please provide valid nonce in Request") {
throw new Exception('Invalid data received (invalid nonce error)');
}
return $dec;
}
function bit2c_get_deposit_address($amount) {
$url = 'https://www.bit2c.co.il/Order/AddCoinFundsRequest';
$req = array();
$req['total'] = $amount;
$req['Coin'] = 'Btc';
$address = bit2c_query($GLOBALS['CONFIG']['bit2c_key'], $GLOBALS['CONFIG']['bit2c_secret'], $url, $req);
return $address["address"];
}
function bit2c_get_balance() {
$url = 'https://www.bit2c.co.il/Account/Balance/v2';
$key = $GLOBALS['CONFIG']['bit2c_key'];
$secret = $GLOBALS['CONFIG']['bit2c_secret'];
return bit2c_query($key, $secret, $url);
}
function bit2c_sell($amount, $rate) {
$url = 'https://www.bit2c.co.il/Order/AddOrder';
$key = $GLOBALS['CONFIG']['bit2c_key'];
$secret = $GLOBALS['CONFIG']['bit2c_secret'];
$req = array();
$req['Amount'] = $amount;
$req['IsBid'] = 'false';
$req['Pair'] = 'BtcNis';
$req['Price'] = $rate;
$req['Total'] = $amount * $rate;
return bit2c_query($key, $secret, $url, $req);
}
function bit2c_buy($amount, $rate) {
$url = 'https://www.bit2c.co.il/Order/AddOrder';
$key = $GLOBALS['CONFIG']['bit2c_key'];
$secret = $GLOBALS['CONFIG']['bit2c_secret'];
$req = array();
$req['Amount'] = $amount;
$req['IsBid'] = 'true';
$req['Pair'] = 'BtcNis';
$req['Price'] = $rate;
$req['Total'] = $amount * $rate;
return bit2c_query($key, $secret, $url, $req);
}
function bit2c_getfee() {
if (isset($GLOBALS['FEES']['bit2c'])) {
return $GLOBALS['FEES']['bit2c'];
} else {
return $GLOBALS['CONFIG']['bit2c_trade_fee'];
}
}
function bit2c_updatefee($fee) {
$GLOBALS['FEES']['bit2c'] = 0 + $fee;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment