Reputation: 185
I am trying to list the current USD price of an item in mBTC (millibitcoin) using the Coinbase API. Here is my code:
<?php
$string = file_get_contents('https://coinbase.com/api/v1/prices/spot_rate');
$result = json_decode($string);
$spot = $result->amount;
$price = 2; //change this to your USD value
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
echo $whole.'.'.$dec.' mBTC';
?>
It works flawlessly in Coderunner (OS X app for development) but fails when run on my hosting server. Link to browser script: http://bitcoindecals.com/oval-price.php
I am using Dynadot Advanced hosting and it includes PHP support. I know that PHP is being utilized because "mBTC" is being echoed correctly. It just appears the $whole
and $dec
variables aren't being set for some reason. Is there a way to get this to work?
Upvotes: 0
Views: 199
Reputation: 11786
You're making a few (extremly wrong) assumptions in the following lines:
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
$price / $spot
is treated as a string and you assume it will be in the format
'0.0019XXXXXXXXXXXX' // 12 x's (unkown numbers)
What if Bitcoin is doing really bad and the rate exeeds 10 mBTC per USD? $price / $spot
will be something like:
'0.0108491827849201'; // (10.8 mBTC)
$whole = substr('0.0108491827849201', 4, -13); // Will be '0'
$dec = substr('0.0108491827849201', 4, -12); // Will be '08'
echo $whole . '.' . $dec . ' mBTC'; // Will echo '0.08 mBTC'
What if, due to rounding or accuracy (! this is what you're seeing in your server - most likely because your OSX is 64bit, your server 32bit or vice-versa !), the string-length of $price / $spot
is less than 18 characters:
'0.0019564521431';
$whole = substr('0.0019564521431', 4, -13);
// Meaning: start at position 4, stop at 13 characters counting from the end
// 13 characters from the end is here: '0.0019564521431'
// ^
// so the stop-position is before the start-position, resulting in an empty
// string. Same with $dec.
echo $whole . '.' . $dec . ' mBTC';
// Will echo empty-string . '.' . empty-string . ' mBTC': '. mBTC'
Long story short: never ever treat numbers as a string (unless you have no other options and you are fully aware of what you are doing). Following code will work, and will give the correct output:
echo number_format($price / $spot * 1000, 1);
// multiply by 1000: BTC to milli-BTC
// , 1: One digit after the dot
For a full explanation of number_format see: http://php.net/number_format
Upvotes: 1
Reputation: 115
you can't crawler the data from https://coinbase.com/api/v1/prices/spot_rate
so $string is empty!
do {
$string = file_get_contents('https://coinbase.com/api/v1/prices/spot_rate');
} while (!empty($string));
$result = json_decode($string);
$spot = $result->amount;
$price = 2; //change this to your USD value
if (isset($spot) or $spot == 0) {
echo "\$spot is not islet or 0";
} else {
$whole = substr($price/$spot, 4, -13);
$dec = substr($price/$spot, 4, -12);
echo $whole.'.'.$dec.' mBTC';
}
Upvotes: 0
Reputation: 580
I just executed the exact code in my WAMP server on windows.... It seemed to work perfectly smooth !!!
OUTPUT : 1.19 mBTC
Is it what you expect????
I think your server is unable to access web pages, so, your php code variables are going vacant...try XAMP and check out ....
Thanks....
Upvotes: 0