jcrimmel Posted October 24, 2022 Share Posted October 24, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315452-formatting-data-returned-in-php-api-call/ Share on other sites More sharing options...
Barand Posted October 24, 2022 Share Posted October 24, 2022 use json_decode( ) on the returned results to get an array like Array ( [everearn] => Array ( [usd] => 1.868E-5 [usd_market_cap] => 0 [usd_24h_vol] => 839.09151161664 [usd_24h_change] => 1.5959917379693 ) ) then you can pick off the values you want with their keys. Quote Link to comment https://forums.phpfreaks.com/topic/315452-formatting-data-returned-in-php-api-call/#findComment-1601892 Share on other sites More sharing options...
jcrimmel Posted October 24, 2022 Author Share Posted October 24, 2022 Thank you Barnad. While I've been using Joomla and editing very basic PHP for many years, I'm just recently starting to dive deeper into the coding side of it. I'm really just learning, so please excuse my ignorance. I assume your supplied code would be placed directly after the $results.... so, something like this? <?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); Array ( [everearn] => Array ( [usd] => 1.868E-5 [usd_market_cap] => 0 [usd_24h_vol] => 839.09151161664 [usd_24h_change] => 1.5959917379693 ) ) $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } Quote Link to comment https://forums.phpfreaks.com/topic/315452-formatting-data-returned-in-php-api-call/#findComment-1601893 Share on other sites More sharing options...
Barand Posted October 24, 2022 Share Posted October 24, 2022 I didn't give you any code. I told you to use json_decode() on the results. What I gave you was the output you will get after using json_decode(); Quote Link to comment https://forums.phpfreaks.com/topic/315452-formatting-data-returned-in-php-api-call/#findComment-1601894 Share on other sites More sharing options...
jcrimmel Posted October 24, 2022 Author Share Posted October 24, 2022 Thanks. I'll try to figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/315452-formatting-data-returned-in-php-api-call/#findComment-1601896 Share on other sites More sharing options...
Barand Posted October 24, 2022 Share Posted October 24, 2022 Come back if you get stuck. Quote Link to comment https://forums.phpfreaks.com/topic/315452-formatting-data-returned-in-php-api-call/#findComment-1601897 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.