How to get HIVE and HBD prices from API with PHP

avatar

Captura de pantalla 2022-08-20 a las 2.11.16.png

code screenshot

Hello guys!, I was looking for a way to get the prices of $HIVE and $HBD via API in PHP for my startup https://cubees.xyz.

I never found anything (maybe I don't ask in the right place, but isn't matter now). Then I found a way to do it with a Simple Request link from the cryptocompare.com API.

But I don't have many skills on development and the response became in a nested array ({"USD":0.5599}) that I needed to decode. So, I tried and tried, and finally I achieve it.

So, if someone were looking for something similar, they can try this code:

<?php 
$url_hive = 'https://min-api.cryptocompare.com/data/price?fsym=HIVE&tsyms=USD'; //URL with a request
$url_hbd = 'https://min-api.cryptocompare.com/data/price?fsym=HBD&tsyms=USD';
$hiveprice = json_decode(file_get_contents($url_hive), true); //Decoding returned json object
$hbdprice = json_decode(file_get_contents($url_hbd), true);

foreach($hiveprice as $a){   //Saving the array nested info to a variable
    echo '<div>'.'<b>HIVE: '.'</b>'.'$'; 
    print_r($a);
    echo '</div>';
    echo '  ';
}

foreach($hbdprice as $b){
    echo '<div>'.'<b>HBD: '.'</b>'.'$';
    print_r($b);
}
echo '</div>';
?>

OUTPUT

image.png

You can also try the @coingecko one:

HIVE: https://api.coingecko.com/api/v3/simple/price?ids=hive&vs_currencies=usd

HBD: https://api.coingecko.com/api/v3/simple/price?ids=hive_dollar&vs_currencies=usd

I know there are many ways to do this with plug-ins, frameworks, etc. But maybe it can help someone.

;)



0
0
0.000
10 comments
avatar

Programming is mind blowing 😋😋😂😂

0
0
0.000
avatar

😅😅 yes

0
0
0.000
avatar

Would love to learn
Just preparing to get a system, the i will learn

0
0
0.000
avatar

I recommend you start with JavaScript or Python 😊

0
0
0.000
avatar

Yea i already did with my phone using pydriod 3, although I don't really enjoy it
System would do better

0
0
0.000
avatar

Yes is too dificult in a phone, you need to try in a PC, for python you just need a dualcore microprocessor and 2gb of ram.

0
0
0.000
avatar

Yeah
I'm trying to save some HBD to get one for myself

0
0
0.000
avatar

Nice piece of code. One suggestion, you could try out is that you don't need to use 2 foreach loops. json_decode when set to TRUE returns an associative array. So you can just call the index of the array. Like:

echo "<b>HIVE : </b>" . $hiveprice['USD'] . "</br>";
echo "<b>HBD : </b>" . $hbdprice['USD']; 

And it will display just like the loops. Happy coding!

0
0
0.000
avatar

Oh, thanks that’s simplify it a lot!!

0
0
0.000