- Edited
Hello all,
I’m looking for some PHP help with understanding how to use DECODE to format the results of an API call. I’m far from being fluent in PHP and have a hard time actually writing it… However, I’ve gotten decent at understanding what a completed script is trying to do when it’s all put together. I know it might be a lot to ask for an answer that is more than “Go study DECODE function”, which has been the response elsewhere and is of course completely unhelpful, but if someone could show me how it would look to use DECODE on one example… I like to think I can learn how to use it on the other API calls I need to work with.
This is one of the API calls I am working with:
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://coingecko.p.rapidapi.com/simple/price?ids=[cb_coingeckoid]&vs_currencies=usd",
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: 3a8c………..………………..b481b"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
This code works great and returns the correct information, however, it’s a bit messy with some extra characters and info. Here are two examples the output results:
Output Example #1:
{"everearn":{"usd":2.39e-05}}
Output Example #2:
{"ethereum":{"usd":1584.67}}
In each of these examples the output should be returned as a formatted price. In Example #1 should read 0.0000239 usd and Example #2 should read 1,584.67 usd.
If there is anyone who would be kind enough to should me what a working code would look like, I’m confident I can adapt that code to work with all of my other API Calls, but I need someone who isn’t going to say “read about the decode function”…. I just don’t learn that way.
Thank you