I’m sure this is pretty easy for most of you, but I am no expert at writing code at all, and I need some help editing this code. I have a PHP API call shown below. The API call is returning data, but it is not formatted correctly.
Thanks in advance.
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://coingecko.p.rapidapi.com/simple/price?ids=everearn&vs_currencies=usd&include_market_cap=true&include_24hr_change=true&include_24hr_vol=true",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: coingecko.p.rapidapi.com",
"X-RapidAPI-Key: 3a8cf65…………………………..4b481b"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
The results of the API call show up formatted as such:
{"everearn":{"usd":1.868e-05,"usd_market_cap":0.0,"usd_24h_vol":839.0915116166398,"usd_24h_change":1.5959917379692774}}
Using the results above, what I would like to do is format/mask the returned data to look like this:
Price: 0.00001868 (Ideally only show the decimals if needed. Some will have 8 decimal places, others will have 2, other prices will reach into xxx,xxx.xx)
24h Change: 1.59% (this could be a negative value as well)
Volume: 839.09
Market Cap: 0.0