Bitcoin Forum
October 23, 2017, 09:17:05 PM *
News: Latest stable version of Bitcoin Core: 0.15.0.1  [Torrent]. (New!)
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Bitcoin PHP currency converter  (Read 1528 times)
BitDave
Newbie
*
Offline Offline

Activity: 1


View Profile
October 17, 2012, 09:17:21 PM
 #1

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?
1508793425
Hero Member
*
Offline Offline

Posts: 1508793425

View Profile Personal Message (Offline)

Ignore
1508793425
Reply with quote  #2

1508793425
Report to moderator
1508793425
Hero Member
*
Offline Offline

Posts: 1508793425

View Profile Personal Message (Offline)

Ignore
1508793425
Reply with quote  #2

1508793425
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1508793425
Hero Member
*
Offline Offline

Posts: 1508793425

View Profile Personal Message (Offline)

Ignore
1508793425
Reply with quote  #2

1508793425
Report to moderator
1508793425
Hero Member
*
Offline Offline

Posts: 1508793425

View Profile Personal Message (Offline)

Ignore
1508793425
Reply with quote  #2

1508793425
Report to moderator
BCB
CTG
VIP
Legendary
*
Offline Offline

Activity: 1036


BCJ


View Profile
October 17, 2012, 09:54:02 PM
 #2

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>';
BCB
CTG
VIP
Legendary
*
Offline Offline

Activity: 1036


BCJ


View Profile
October 17, 2012, 10:28:04 PM
 #3

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)
BCB
CTG
VIP
Legendary
*
Offline Offline

Activity: 1036


BCJ


View Profile
October 17, 2012, 10:29:19 PM
 #4

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)
alexanderrrr
Newbie
*
Offline Offline

Activity: 27


View Profile
November 05, 2012, 05:58:49 AM
 #5

Thanks for the codesnippets. Useful.
svirus
Member
**
Offline Offline

Activity: 72


View Profile WWW
November 08, 2012, 11:27:15 AM
 #6

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);

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!