Bitcoin Forum

Other => Beginners & Help => Topic started by: BitDave on October 17, 2012, 09:17:21 PM



Title: Bitcoin PHP currency converter
Post by: BitDave on October 17, 2012, 09:17:21 PM
I want to accept bitcoins on my ecommerce site for payment of goods. I'll accept them manually for now, generating an address for each customer, but as a minimum I need to display the visitor's checkout total in both USD and bitcoins. Is there a way of getting data from a server on what the current bitcoin exchange rate is via PHP?


Title: Re: Bitcoin PHP currency converter
Post by: BCB on October 17, 2012, 09:54:02 PM
Code:
function mtgox_query($path, array $req = array()) {
$key = 'xxx';                                   //from your mt. gox account
$secret = 'xxxx'; //from your mt. gox account
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
$post_data = http_build_query($req, '', '&');
$headers = array(
'Rest-Key: '.$key,
'Rest-Sign: '.base64_encode(hash_hmac('sha512', $post_data, base64_decode($secret), true)),
);

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; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, 'https://mtgox.com/api/'.$path);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$res = curl_exec($ch);

if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec = json_decode($res, true);



if (!$dec) throw new Exception('mtgox_query');
return $dec;
}

//call the method from (https://en.bitcoin.it/wiki/MtGox/API/HTTP/v1) using the function.
$mt_gox = mtgox_query('1/BTCUSD/ticker');


//out put the value.
echo $mt_gox['return']['last']['value'];

//this will dump all the values being returned from this call.
echo '<pre>' . print_r($mt_gox,true) . '</pre>';


Title: Re: Bitcoin PHP currency converter
Post by: BCB on October 17, 2012, 10:28:04 PM
You could also be using

Code:
function JSONtoAmount($value) {
    return round($value * 1e8);
}


to convert floats and avoid rounding errors.

This is explained here.

https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)


Title: Re: Bitcoin PHP currency converter
Post by: BCB on October 17, 2012, 10:29:19 PM
You could also be using

Code:
function JSONtoAmount($value) {
    return round($value * 1e8);
}


to convert floats and avoid rounding errors.

or just use the mount gox "value_int" value and convert to decimal for display.

This is explained here.

https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)


Title: Re: Bitcoin PHP currency converter
Post by: alexanderrrr on November 05, 2012, 05:58:49 AM
Thanks for the codesnippets. Useful.


Title: Re: Bitcoin PHP currency converter
Post by: svirus on November 08, 2012, 11:27:15 AM
From php 5.0.0 you can change this
Code:
        $mt = explode(' ', microtime());
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
to
Code:
         $req['nonce'] = microtime(true);