PHP Tutorial: How To Convert JSON API Request
Converting JSON API Data To PHP Array
Using current STEEM price from Coin Market Cap API for a form by parsing data into a PHP array, then assigning to a variable.
Parsing the Data
First, you'll want to read your JSON output by visiting the link you will get your data from. Determine if it is a multi-dimensional array or just a single array of values. Also, look for the key-value pairs you will be using; price_usd in our case.
Decode the JSON data and reference your key-value pairs using the multi-dimensional array and assign the data to a variable (if only using one pair).
$steemprice = $steemjson[0][price_usd];
Multiple data sets can be referenced in the same way just using the array variable reference [and a for loop].
//Steem price in BTC
echo $steemjson[0][price_btc];
// 1 hour change in percentage
echo $steemjson[0][percent_change_1h];
Spit Out The Results
You can use echo, print or print_r to display your results on screen in PHP. There is another shortcut that we used in the video.
<?=$steemprice?>
- same as -
<? echo $steemprice; ?>
Another method, print_r is used like var_dump, it outputs all the data from the array.
References:
Coin Market Cap API: https://coinmarketcap.com/currencies/steem/#tools
File Used on CoinThru.com: https://cointhru.com/steem/income-calculator.php
Thanks for watching. Follow me for more tutorials and other posts about cryptocurrency, life, etc... As always, feedback is appreciated, comments and questions are welcome.
@tunnelrat got you a $1.55 @minnowbooster upgoat, nice! (Image: pixabay.com)
Want a boost? Click here to read more!
hi i try, load json form comment and i got error
5209: Uncaught TypeError: Cannot read property 'responseText' of undefined
function setup() { loadJSON ("https://steemit.com/streem/@moderators/20180224t095515444z-post.json",gotData, 'jsonp'); }