GoldCoin Talk
Main Category => Project Development => Topic started by: MicroGuy on February 13, 2018, 10:18:23 PM
-
I'm working on a script that will slowly accumulate GLD over time. However the following script is using GET and I need to use POST method:
API Docs:
https://tradesatoshi.com/Home/Api
Screenshot 1 >> https://i.stack.imgur.com/8OjTk.png
Screenshot 2 >> https://i.stack.imgur.com/C3EJr.png
Script:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
// Generate random amount between 28.00 and 30.00
$ran1 = mt_rand(2800, 3000);
$ran2 = $ran1/100;
// Config
$sAPIKey = ""; // private key
$sAPISecret = ""; // api secret
$sPair = "GLD_BTC";
$sRate = "0.0001";
$sNonce = time();
// Request
$sRequest = "https://someexchange/api/private/submitorder?API_KEY=" . $sAPIKey . "&Market=" . $sPair . "&Type=" . 'Buy' . "&Amount=" . $ran2 . "&Price=" . $sRate . "&nonce=" . $sNonce;
echo $sRequest;
$sSign=hash_hmac('sha512', $sRequest, $sAPISecret);
$ch = curl_init($sRequest);
// disable cert validation
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // shouldn't need this
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:' . $sSign));
$execResult = curl_exec($ch);
?>
Conversion to POST anyone? :think:
-
echo ($_POST ['$sRequest']);
Or
echo ($_POST ["$sRequest"]);
GoldRush :)
-
echo ($_POST ['$sRequest']);
Or
echo ($_POST ["$sRequest"]);
GoldRush :)
That echo was only added as a trouble-shooting aid.
-
I think this should work;
Replace this line
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:' . $sSign));
With these lines
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('apisign:' . $sSign));
-
I think this should work;
Replace this line
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:' . $sSign));
With these lines
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('apisign:' . $sSign));
Thank you! This change made the GET error disappear. Now when I run the script it completes without error but also without trades? :think:
-
Here's the latest version of the script:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
// Generate random amount between 28.00 and 30.00
$ran1 = mt_rand(2800, 3000);
$ran2 = $ran1/100;
// Config
$sAPIKey = ""; // private key
$sAPISecret = ""; // api secret
$sPair = "GLD_BTC";
$sRate = "0.0001";
$sNonce = time();
// Request
$sRequest = "https://someexchange/api/private/submitorder?API_KEY=" . $sAPIKey . "&Market=" . $sPair . "&Type=" . 'Buy' . "&Amount=" . $ran2 . "&Price=" . $sRate . "&nonce=" . $sNonce;
echo $sRequest;
$sSign=hash_hmac('sha512', $sRequest, $sAPISecret);
$ch = curl_init($sRequest);
// disable cert validation
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // shouldn't need this
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('apisign:' . $sSign));
$execResult = curl_exec($ch);
?>
~~
Let's try a bounty. Paying 250 GLD for the first solution posted below! :)
-
OK, registered at TradeSatoshi to get an API key so I could see what was going on.
There was a nice start here https://github.com/WorldBot/Trade-Satoshi-API, which I slightly modified.
You need two files for this;
First file is where the magic happens, copy it from the code below and enter your own API KEY and SECRET.
Save it as 'api_tradesatoshi.php'.
<?php
function api_query($ENDPOINT, array $REQ = array())
{
/* FILL IN YOUR API KEY AND SECRET */
$API_PUBLIC_KEY = 'YOURAPIKEY'; // Your Public Api Key
$API_SECRET_KEY = 'YOURAPISECRET'; // Your Private Api Key
/* DO NOT CHANGE ANYTHING BELOW THIS LINE */
$PUBLIC_API = array('GetCurrencies','GetTicker','GetMarketHistory','GetMarketSummary','GetMarketSummaries','GetOrderBook');
$PRIVATE_API = array('GetBalance','GetBalances','GetOrder','GetOrders','SubmitOrder','CancelOrder','GetTradeHistory','GenerateAddress','SubmitWithdraw','GetDeposits','GetWithdrawals');
// Init curl
static $ch = null; $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; TradeSatoshi API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
// remove those 2 line to secure after test.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// PUBLIC or PRIVATE API
if (in_array($ENDPOINT,$PUBLIC_API))
{
$URL = "https://tradesatoshi.com/api/public/".strtolower($ENDPOINT);
if ($REQ) $URL .= '?'.http_build_query($REQ,'','&');
curl_setopt($ch, CURLOPT_URL, $URL );
}
elseif (in_array($ENDPOINT,$PRIVATE_API))
{
$URL = "https://tradesatoshi.com/api/private/".strtolower($ENDPOINT);
$mt = explode(' ', microtime()); $NONCE = $mt[1].substr($mt[0], 2, 6);
$REQ = json_encode($REQ);
$SIGNATURE = $API_PUBLIC_KEY.'POST'.strtolower(urlencode($URL)).$NONCE.base64_encode($REQ);
$HMAC_SIGN = base64_encode(hash_hmac('sha512',$SIGNATURE,base64_decode($API_SECRET_KEY),true));
$HEADER = 'Basic '.$API_PUBLIC_KEY.':'.$HMAC_SIGN.':'.$NONCE;
$HEADERS = array("Content-Type: application/json; charset=utf-8", "Authorization: $HEADER");
curl_setopt($ch, CURLOPT_HTTPHEADER, $HEADERS);
curl_setopt($ch, CURLOPT_URL, $URL );
curl_setopt($ch, CURLOPT_POSTFIELDS,$REQ);
}
// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
return json_decode($res);
}
/* ---------- Public Functions ---------- */
function GetCurrencies()
{return api_query("GetCurrencies");}
function GetTicker($market=false)
{if($market===false)return (object)array("success"=>false,"message"=>'GetTicker: market name e.g. "LTC_BTC" (required)'); else return api_query("GetTicker",array('market'=>$market));}
function GetMarketHistory($market=false,$count=20)
{if($market===false)return (object)array("success"=>false,"message"=>'GetMarketHistory: market name e.g. "LTC_BTC" (required)'); else return api_query("GetMarketHistory",array('market'=>$market,'count'=>$count));}
function GetMarketSummary($market=false)
{if($market===false)return (object)array("success"=>false,"message"=>'GetMarketSummary: market name e.g. "LTC_BTC" (required)'); else return api_query("GetMarketSummary",array('Market'=>$market));}
function GetMarketSummaries()
{return api_query("GetMarketSummaries");}
function GetOrderBook($market=false,$type='both',$depth=20)
{if($market===false)return (object)array("success"=>false,"message"=>'GetOrderBook: market name e.g. "LTC_BTC" (required)'); else return api_query("GetOrderBook",array('Market'=>$market,'Type'=>$type,'Depth'=>$depth));}
/* ---------- Privates Functions ---------- */
/* GetBalance
Currency: The currency of the balance to return e.g. 'BTC' (required)
*/
function GetBalance($currency=false)
{if($currency===false)return (object)array("success"=>false,"message"=>'GetBalance: currency name e.g. "BTC" (required)'); else return api_query("GetBalance",array('Currency'=>$currency));}
// No Params
function GetBalances()
{return api_query("GetBalances");}
/* GetOrder
OrderId: The order to return (required)
*/
function GetOrder($orderid=false)
{if($orderid===false)return (object)array("success"=>false,"message"=>'GetOrder: OrderId required!'); else return api_query("GetOrder",array('OrderId'=>$orderid));}
/* GetOrders
Market: The market name e.g. 'LTC_BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, deefault: 20)
*/
function GetOrders($market="all",$count=20)
{return api_query("GetOrders",array('Market'=>$market,'Count'=>$count));}
/* SubmitOrder
Market: The market name e.g. 'LTC_BTC' (required)
Type: The order type name e.g. 'Buy', 'Sell' (required)
Amount: The amount to buy/sell (required)
Price: The price to buy/sell for (required)
*/
function SubmitOrder($market=false,$type=false,$amount=false,$price=false)
{if($market===false||$type===false||$amount===false||$price===false)return (object)array("success"=>false,"message"=>'SubmitOrder: Market,Type,Amount,Price are required!'); else return api_query("SubmitOrder",array('Market'=>$market,'Type'=>$type,'Amount'=>$amount,'Price'=>$price));}
/* CancelOrder
Type: The cancel type, options: 'Single','Market','MarketBuys','MarketSells','AllBuys','AllSells','All'(required)
OrderId: The order to cancel(required if cancel type 'Single')
Market: The order to cancel(required if cancel type 'Market','MarketBuys','MarketSells')
*/
function CancelOrder($type=false,$orderid=false,$market=false)
{if($type===false)return (object)array("success"=>false,"message"=>'CancelOrder: Type and/or OrderId and/or Market are required!'); else return api_query("CancelOrder",array('Type'=>$type,'OrderId'=>$orderid,'Market'=>$market));}
/* GetTradeHistory
Market: The market name e.g. 'LTC_BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, default: 20)
*/
function GetTradeHistory($market='all',$count=20)
{return api_query("GetTradeHistory",array('Market'=>$market,'Count'=>$count));}
/* GenerateAddress
Currency: The currency to generate address for e.g. 'BTC' (required)
*/
function GenerateAddress($currency=false)
{if($currency===false)return (object)array("success"=>false,"message"=>'GenerateAddress: The currency to generate address for e.g. "BTC" (required)'); else return api_query("GenerateAddress",array('Currency'=>$currency));}
/* SubmitWithdraw
Currency: The currency name e.g. 'BTC' (required)
Address: The receiving address (required)
Amount: The amount to withdraw (required)
*/
function SubmitWithdraw($currency=false,$address=false,$amount=false)
{if($currency===false||$address===false||$amount===false)return (object)array("success"=>false,"message"=>'SubmitWithdraw: Currency,Address,Amount are required'); else return api_query('SubmitWithdraw',array('Currency'=>$currency,'Address'=>$address,'Amount'=>$amount));}
/* GetDeposits
Currency: The currency name e.g. 'BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, default: 20)
*/
function GetDeposits($currency='all',$count=20)
{return api_query('GetDeposits',array('Currency'=>$currency,'Count'=>$count));}
/* GetWithdrawals
Currency: The currency name e.g. 'BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, default: 20)
*/
function GetWithdrawals($currency='all',$count=20)
{return api_query('GetWithdrawals',array('Currency'=>$currency,'Count'=>$count));}
?>
Second file, save it as 'buy.php' or whatever you like to name it.
<?php
// feed $API_PUBLIC_KEY & $API_SECRET_KEY in api_tradesatoshi.php file
@include('api_tradesatoshi.php');
// Generate random amount between 28.00 and 30.00
$ran = mt_rand(2800, 3000)/100;
// Set the pair
$sPair = "GLD_BTC";
// Set price in BTC
$sPrice = "0.00001526";
// Set action type
$sType = "'Buy'"; //change to 'Sell' for sell order (include '')
if(isset($_POST["buy"]))
{
SubmitOrder($sPair,$sType,$ran,$sPrice);
echo "Submitted if there are funds => Pair: ".$sPair." Action: ".$sType." Amount: ".$ran." Price: ".$sPrice;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form action="" method="post" name="frm_buy">
<input type="submit" name="buy" value="buy"/>
</form>
</body>
</html>Make sure both files are in the same folder and open buy.php in your browser.
Hit the buy button, if there are funds the buy is submitted, otherwise not.
You can see the notification when hitting 'buy' on the main TradeSatoshi site.
I would only get 'Insufficient Funds' messages since I have no BTC there.
Hope this works for you :thumbsup:
-
OK, registered at TradeSatoshi to get an API key so I could see what was going on.
There was a nice start here https://github.com/WorldBot/Trade-Satoshi-API, which I slightly modified.
You need two files for this;
First file is where the magic happens, copy it from the code below and enter your own API KEY and SECRET.
Save it as 'api_tradesatoshi.php'.
<?php
function api_query($ENDPOINT, array $REQ = array())
{
/* FILL IN YOUR API KEY AND SECRET */
$API_PUBLIC_KEY = 'YOURAPIKEY'; // Your Public Api Key
$API_SECRET_KEY = 'YOURAPISECRET'; // Your Private Api Key
/* DO NOT CHANGE ANYTHING BELOW THIS LINE */
$PUBLIC_API = array('GetCurrencies','GetTicker','GetMarketHistory','GetMarketSummary','GetMarketSummaries','GetOrderBook');
$PRIVATE_API = array('GetBalance','GetBalances','GetOrder','GetOrders','SubmitOrder','CancelOrder','GetTradeHistory','GenerateAddress','SubmitWithdraw','GetDeposits','GetWithdrawals');
// Init curl
static $ch = null; $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; TradeSatoshi API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
// remove those 2 line to secure after test.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// PUBLIC or PRIVATE API
if (in_array($ENDPOINT,$PUBLIC_API))
{
$URL = "https://tradesatoshi.com/api/public/".strtolower($ENDPOINT);
if ($REQ) $URL .= '?'.http_build_query($REQ,'','&');
curl_setopt($ch, CURLOPT_URL, $URL );
}
elseif (in_array($ENDPOINT,$PRIVATE_API))
{
$URL = "https://tradesatoshi.com/api/private/".strtolower($ENDPOINT);
$mt = explode(' ', microtime()); $NONCE = $mt[1].substr($mt[0], 2, 6);
$REQ = json_encode($REQ);
$SIGNATURE = $API_PUBLIC_KEY.'POST'.strtolower(urlencode($URL)).$NONCE.base64_encode($REQ);
$HMAC_SIGN = base64_encode(hash_hmac('sha512',$SIGNATURE,base64_decode($API_SECRET_KEY),true));
$HEADER = 'Basic '.$API_PUBLIC_KEY.':'.$HMAC_SIGN.':'.$NONCE;
$HEADERS = array("Content-Type: application/json; charset=utf-8", "Authorization: $HEADER");
curl_setopt($ch, CURLOPT_HTTPHEADER, $HEADERS);
curl_setopt($ch, CURLOPT_URL, $URL );
curl_setopt($ch, CURLOPT_POSTFIELDS,$REQ);
}
// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
return json_decode($res);
}
/* ---------- Public Functions ---------- */
function GetCurrencies()
{return api_query("GetCurrencies");}
function GetTicker($market=false)
{if($market===false)return (object)array("success"=>false,"message"=>'GetTicker: market name e.g. "LTC_BTC" (required)'); else return api_query("GetTicker",array('market'=>$market));}
function GetMarketHistory($market=false,$count=20)
{if($market===false)return (object)array("success"=>false,"message"=>'GetMarketHistory: market name e.g. "LTC_BTC" (required)'); else return api_query("GetMarketHistory",array('market'=>$market,'count'=>$count));}
function GetMarketSummary($market=false)
{if($market===false)return (object)array("success"=>false,"message"=>'GetMarketSummary: market name e.g. "LTC_BTC" (required)'); else return api_query("GetMarketSummary",array('Market'=>$market));}
function GetMarketSummaries()
{return api_query("GetMarketSummaries");}
function GetOrderBook($market=false,$type='both',$depth=20)
{if($market===false)return (object)array("success"=>false,"message"=>'GetOrderBook: market name e.g. "LTC_BTC" (required)'); else return api_query("GetOrderBook",array('Market'=>$market,'Type'=>$type,'Depth'=>$depth));}
/* ---------- Privates Functions ---------- */
/* GetBalance
Currency: The currency of the balance to return e.g. 'BTC' (required)
*/
function GetBalance($currency=false)
{if($currency===false)return (object)array("success"=>false,"message"=>'GetBalance: currency name e.g. "BTC" (required)'); else return api_query("GetBalance",array('Currency'=>$currency));}
// No Params
function GetBalances()
{return api_query("GetBalances");}
/* GetOrder
OrderId: The order to return (required)
*/
function GetOrder($orderid=false)
{if($orderid===false)return (object)array("success"=>false,"message"=>'GetOrder: OrderId required!'); else return api_query("GetOrder",array('OrderId'=>$orderid));}
/* GetOrders
Market: The market name e.g. 'LTC_BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, deefault: 20)
*/
function GetOrders($market="all",$count=20)
{return api_query("GetOrders",array('Market'=>$market,'Count'=>$count));}
/* SubmitOrder
Market: The market name e.g. 'LTC_BTC' (required)
Type: The order type name e.g. 'Buy', 'Sell' (required)
Amount: The amount to buy/sell (required)
Price: The price to buy/sell for (required)
*/
function SubmitOrder($market=false,$type=false,$amount=false,$price=false)
{if($market===false||$type===false||$amount===false||$price===false)return (object)array("success"=>false,"message"=>'SubmitOrder: Market,Type,Amount,Price are required!'); else return api_query("SubmitOrder",array('Market'=>$market,'Type'=>$type,'Amount'=>$amount,'Price'=>$price));}
/* CancelOrder
Type: The cancel type, options: 'Single','Market','MarketBuys','MarketSells','AllBuys','AllSells','All'(required)
OrderId: The order to cancel(required if cancel type 'Single')
Market: The order to cancel(required if cancel type 'Market','MarketBuys','MarketSells')
*/
function CancelOrder($type=false,$orderid=false,$market=false)
{if($type===false)return (object)array("success"=>false,"message"=>'CancelOrder: Type and/or OrderId and/or Market are required!'); else return api_query("CancelOrder",array('Type'=>$type,'OrderId'=>$orderid,'Market'=>$market));}
/* GetTradeHistory
Market: The market name e.g. 'LTC_BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, default: 20)
*/
function GetTradeHistory($market='all',$count=20)
{return api_query("GetTradeHistory",array('Market'=>$market,'Count'=>$count));}
/* GenerateAddress
Currency: The currency to generate address for e.g. 'BTC' (required)
*/
function GenerateAddress($currency=false)
{if($currency===false)return (object)array("success"=>false,"message"=>'GenerateAddress: The currency to generate address for e.g. "BTC" (required)'); else return api_query("GenerateAddress",array('Currency'=>$currency));}
/* SubmitWithdraw
Currency: The currency name e.g. 'BTC' (required)
Address: The receiving address (required)
Amount: The amount to withdraw (required)
*/
function SubmitWithdraw($currency=false,$address=false,$amount=false)
{if($currency===false||$address===false||$amount===false)return (object)array("success"=>false,"message"=>'SubmitWithdraw: Currency,Address,Amount are required'); else return api_query('SubmitWithdraw',array('Currency'=>$currency,'Address'=>$address,'Amount'=>$amount));}
/* GetDeposits
Currency: The currency name e.g. 'BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, default: 20)
*/
function GetDeposits($currency='all',$count=20)
{return api_query('GetDeposits',array('Currency'=>$currency,'Count'=>$count));}
/* GetWithdrawals
Currency: The currency name e.g. 'BTC' (optional, default: 'all')
Count: The maximum count of records to return (optional, default: 20)
*/
function GetWithdrawals($currency='all',$count=20)
{return api_query('GetWithdrawals',array('Currency'=>$currency,'Count'=>$count));}
?>
Second file, save it as 'buy.php' or whatever you like to name it.
<?php
// feed $API_PUBLIC_KEY & $API_SECRET_KEY in api_tradesatoshi.php file
@include('api_tradesatoshi.php');
// Generate random amount between 28.00 and 30.00
$ran = mt_rand(2800, 3000)/100;
// Set the pair
$sPair = "GLD_BTC";
// Set price in BTC
$sPrice = "0.00001526";
// Set action type
$sType = "'Buy'"; //change to 'Sell' for sell order (include '')
if(isset($_POST["buy"]))
{
SubmitOrder($sPair,$sType,$ran,$sPrice);
echo "Submitted if there are funds => Pair: ".$sPair." Action: ".$sType." Amount: ".$ran." Price: ".$sPrice;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form action="" method="post" name="frm_buy">
<input type="submit" name="buy" value="buy"/>
</form>
</body>
</html>Make sure both files are in the same folder and open buy.php in your browser.
Hit the buy button, if there are funds the buy is submitted, otherwise not.
You can see the notification when hitting 'buy' on the main TradeSatoshi site.
I would only get 'Insufficient Funds' messages since I have no BTC there.
Hope this works for you :thumbsup:
Thank you. I'll give this a try tonight! :)
~~
Note: I need to run this automatically. I think I'll be able to figure that part of the code out myself. Will update the thread soon ....
-
Yeah, just remove the html form, and the if statement from the buy.php. And leave in the "SubmitOrder($sPair,$sType,$ran,$sPrice);"
Then just run buy.php to buy, no clicking any buttons.
-
Yeah, just remove the html form, and the if statement from the buy.php. And leave in the "SubmitOrder($sPair,$sType,$ran,$sPrice);"
Then just run buy.php to buy, no clicking any buttons.
Thanks. Everything is working great. :)
~~
Going to have some fun tonight adding a few bells and whistles!