Author

Topic: [WTB] Bitstamp PHP<->API solution for.... (Read 313 times)

full member
Activity: 221
Merit: 100
August 20, 2017, 07:48:41 AM
#10
Now  Smiley This code "just" gets the balance no trades or buys are made  Sad

Seems the "buy" part dosen't do what's intended. No error message now.

Code:
// Amount of USD to buy
$bitstamp_usd_amount=10;

// Bitcoin address to send to
//$bitstamp_bitcoin_address="DESTINATION_BITCOIN_ADDRESS_HERE";


$bitstamp_eurusd=bitstamp_query("eur_usd",array(),"get");
$bitstamp_eur_amount=$bitstamp_usd_amount/$bitstamp_eurusd["buy"];

$bitstamp_buy_order=bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
$bitstamp_ordered_amount=$bitstamp_buy_order["amount"];

//print_r(bitstamp_query("bitcoin_withdrawal",array(
// "amount" => $bitstamp_ordered_amount,
// "address" => $bitstamp_bitcoin_address,
// "instant" => 0 // set to 1 to send a BitGo instant payment. An additional fee apply.
//)));


// get balance
print_r(bitstamp_query("balance",array()));
hero member
Activity: 896
Merit: 527
₿₿₿₿₿₿₿
August 20, 2017, 07:05:07 AM
#9
Are you using the code from my post or the code from your post? I fixed a small glitch since I posted the code which may be the culprit.

EDIT: Yes, you're using the version with the glitch.

This is the line with the problem:
Code:
$bitstamp_eurusd=bitstamp_query("eur_usd");

You must use this instead:

Code:
$bitstamp_eurusd=bitstamp_query("eur_usd",array(),"get");
full member
Activity: 221
Merit: 100
August 20, 2017, 07:00:56 AM
#8
OK.. Rolling back a bit

With you initial code, an API which gives 100% access. I can get the balance of my account.
But when I run this code:

Code:
// Amount of USD to buy
$bitstamp_usd_amount=10;

// Bitcoin address to send to
//$bitstamp_bitcoin_address="DESTINATION_BITCOIN_ADDRESS_HERE";


$bitstamp_eurusd=bitstamp_query("eur_usd");
$bitstamp_eur_amount=$bitstamp_usd_amount/$bitstamp_eurusd["buy"];

$bitstamp_buy_order=bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
$bitstamp_ordered_amount=$bitstamp_buy_order["amount"];

//print_r(bitstamp_query("bitcoin_withdrawal",array(
// "amount" => $bitstamp_ordered_amount,
// "address" => $bitstamp_bitcoin_address,
// "instant" => 0 // set to 1 to send a BitGo instant payment. An additional fee apply.
//)));


// get balance
print_r(bitstamp_query("balance",array()));

This is the outcome

Code:
Warning: Division by zero in /home/www/kk-webdesign.dk/krak/test.php on line 53
Array ( [xrp_available] => 0.00000000 [eur_available] => 87.19 [usd_reserved] => 0.00 [eur_balance] => 87.19 [btc_balance] => 0.00572782 [usd_available] => 28.95 [btc_reserved] => 0.00000000 [fee] => 0.2500 [btc_available] => 0.00572782 [eur_reserved] => 0.00 [xrp_reserved] => 0.00000000 [xrp_balance] => 0.00000000 [usd_balance] => 28.95 )
hero member
Activity: 896
Merit: 527
₿₿₿₿₿₿₿
August 20, 2017, 06:37:32 AM
#7
They have both markets available. I did USD>EUR conversion because that's what you asked.
This is all that's needed:

Code:
$bitstamp_eur_amount=10;
bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
full member
Activity: 221
Merit: 100
August 20, 2017, 06:29:30 AM
#6
OK... Now if I remove the USD/EUR conversion and disabling the withdrawal by doing this:

Code:
// Amount of USD to buy
//$bitstamp_usd_amount=10;
// Amount of EUR to buy
$bitstamp_eur_amount=10;

// Bitcoin address to send to
//$bitstamp_bitcoin_address="DESTINATION_BITCOIN_ADDRESS_HERE";


//$bitstamp_eurusd=bitstamp_query("eur_usd");
//$bitstamp_eur_amount=$bitstamp_usd_amount/$bitstamp_eurusd["buy"];

$bitstamp_buy_order=bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
$bitstamp_ordered_amount=$bitstamp_buy_order["amount"];

//print_r(bitstamp_query("bitcoin_withdrawal",array(
// "amount" => $bitstamp_ordered_amount,
// "address" => $bitstamp_bitcoin_address,
// "instant" => 0 // set to 1 to send a BitGo instant payment. An additional fee apply.
//)));


// get balance
//print_r(bitstamp_query("balance",array()));

That shoud also work correct, or does bitstamp only allow buy_market with USD in their API?
hero member
Activity: 896
Merit: 527
₿₿₿₿₿₿₿
August 20, 2017, 06:01:39 AM
#5
The actual Market Buy is not included on print_r() because there is no need to output the information from it. We need to process the information from the variable instead.
full member
Activity: 221
Merit: 100
August 20, 2017, 05:54:09 AM
#4
Code:
$bitstamp_eurusd=bitstamp_query("eur_usd");
$bitstamp_eur_amount=$bitstamp_usd_amount/$bitstamp_eurusd["buy"];

$bitstamp_buy_order=bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
$bitstamp_ordered_amount=$bitstamp_buy_order["amount"];

Before I start doing stuff, does this part not require a
Code:
print_r(bitstamp_que.......)?
I understand the conversion between USD<->EUR, but it seems that the "action" to buy dosen't exist?
full member
Activity: 221
Merit: 100
August 20, 2017, 04:10:22 AM
#3
Not the most elegant solution, but should work. You have to replace the key, secret and client id on the first line.
Of course you also need to specify the amount of USD you want to buy and the destination Bitcoin address.

Code:
function bitstamp_query($path, array $req = array(), $verb = 'post', $key="KEY_HERE", $secret="SECRET_HERE", $client_id="CLIENT_ID_HERE"){
        // 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['key'] = $key;
        $req['signature'] = bitstamp_get_signature($req['nonce'],$key,$secret,$client_id);

        // generate the POST data string
$post_data = http_build_query($req, '', '&');
// any extra headers
$headers = array();

// our curl handle (initialize if required)
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; BitStamp PHP Client');
}
curl_setopt($ch, CURLOPT_URL, 'https://www.bitstamp.net/api/' . $path .'/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);  // man-in-the-middle defense by verifying ssl cert.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);  // man-in-the-middle defense by verifying ssl cert.
if ($verb == 'post'){
       curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// run the query
$res = curl_exec($ch);
if ($res === false)
       return false;
$dec = json_decode($res, true);
if (is_null($dec))
       return false;
return $dec;
}
function bitstamp_get_signature($nonce,$key,$secret,$client_id){
$message = $nonce.$client_id.$key;
return strtoupper(hash_hmac('sha256', $message, $secret));
}



// Amount of USD to buy
$bitstamp_usd_amount=USD_AMOUNT_HERE;

// Bitcoin address to send to
$bitstamp_bitcoin_address="DESTINATION_BITCOIN_ADDRESS_HERE";


$bitstamp_eurusd=bitstamp_query("eur_usd");
$bitstamp_eur_amount=$bitstamp_usd_amount/$bitstamp_eurusd["buy"];

$bitstamp_buy_order=bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
$bitstamp_ordered_amount=$bitstamp_buy_order["amount"];

print_r(bitstamp_query("bitcoin_withdrawal",array(
"amount" => $bitstamp_ordered_amount,
"address" => $bitstamp_bitcoin_address,
"instant" => 0 // set to 1 to send a BitGo instant payment. An additional fee apply.
)));


// get balance
print_r(bitstamp_query("balance",array()));

Derived from a class made for BitStamp which I couldn't get to work.

Will try this later today Smiley
hero member
Activity: 896
Merit: 527
₿₿₿₿₿₿₿
August 20, 2017, 03:22:02 AM
#2
Not the most elegant solution, but should work. You have to replace the key, secret and client id on the first line.
Of course you also need to specify the amount of USD you want to buy and the destination Bitcoin address.

Code:
/**
* BitStamp Order and Withdraw
* https://bitcointalk.org/index.php?topic=2102954.msg21033624#msg21033624
**/
function bitstamp_query($path, array $req = array(), $verb 'post'$key="KEY_HERE"$secret="SECRET_HERE"$client_id="CLIENT_ID_HERE"){
        
// 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], 26);
        
$req['key'] = $key;
        
$req['signature'] = bitstamp_get_signature($req['nonce'],$key,$secret,$client_id);

        
// generate the POST data string
$post_data http_build_query($req'''&');
// any extra headers
$headers = array();

// our curl handle (initialize if required)
static $ch null;
if (is_null($ch)){
        $ch curl_init();
        curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        curl_setopt($chCURLOPT_USERAGENT'Mozilla/4.0 (compatible; BitStamp PHP Client');
}
curl_setopt($chCURLOPT_URL'https://www.bitstamp.net/api/' $path .'/');
curl_setopt($chCURLOPT_SSL_VERIFYPEER1);  // man-in-the-middle defense by verifying ssl cert.
curl_setopt($chCURLOPT_SSL_VERIFYHOST2);  // man-in-the-middle defense by verifying ssl cert.
if ($verb == 'post'){
        curl_setopt($chCURLOPT_POSTFIELDS$post_data);
}
curl_setopt($chCURLOPT_HTTPHEADER$headers);
// run the query
$res curl_exec($ch);
if ($res === false)
        return false;
$dec json_decode($restrue);
if (is_null($dec))
        return false;
return $dec;
}
function 
bitstamp_get_signature($nonce,$key,$secret,$client_id){
$message $nonce.$client_id.$key;
return strtoupper(hash_hmac('sha256'$message$secret));
}



// Amount of USD to buy
$bitstamp_usd_amount=USD_AMOUNT_HERE;

// Bitcoin address to send to
$bitstamp_bitcoin_address="DESTINATION_BITCOIN_ADDRESS_HERE";


$bitstamp_eurusd=bitstamp_query("eur_usd",array(),"get");
$bitstamp_eur_amount=$bitstamp_usd_amount/$bitstamp_eurusd["buy"];

$bitstamp_buy_order=bitstamp_query("v2/buy/market/btceur",array("amount"=>$bitstamp_eur_amount));
$bitstamp_ordered_amount=$bitstamp_buy_order["amount"];

print_r(bitstamp_query("bitcoin_withdrawal",array(
"amount" => $bitstamp_ordered_amount,
"address" => $bitstamp_bitcoin_address,
"instant" => // set to 1 to send a BitGo instant payment. An additional fee apply.
)));


// get balance
print_r(bitstamp_query("balance",array()));

My Bitcoin address: 3KRvcgq8odGDN4DXN9iLpEMnE4mseVbmyP

Derived from a class made for BitStamp.
full member
Activity: 221
Merit: 100
August 19, 2017, 12:04:00 PM
#1
creating an automatic buy $amount of bitcoins in €EUR and withdraw this purchase to a bitcoin wallet

Hi!

Trying to create a small business without have a lot of knowledge about API and PHP. I have a 100% working buy script for the Kraken API, but their service does not offer a way to withdraw btc to an external chosen wallet. That's why I need a bitstamp solution...

What I need is the following:

A single file script which handles the authentication, the market buy of x EUR and then withdraw's to a specified btc wallet, in short:

auth->buy_market->withdraw

I have looked at the wrappers available without success. Now I have an offer from freelancer.com where a few companies/individuals are offering such a solution for the price between $50 and $150 and quite a few which is above $300

Now what I would like is to help out anybody else in my situation.

The first comment to this thread which delivers a code where I only have to enter the Bitstamp necassaries (key, secret, client_id) the amount as a $amount and $wallet_address for the withdrawal wallet gets 0.025 btc. This is not a lot of money, but in this way others may learn and I give back to a community.

A small wish for the code would also be that it allows for anybody to do simple a simple "echo $my_bitstamp_balance;" in order to verify their balance.

Along with your code in this tread please post your btc address for your reward.

Thank you all!
Jump to: