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

    Welcome to the forums. I edited your post to wrap the code in this forum's [code]...[/code] tags, as that works better for a block of code than the </> widget in the edit window tools.

    PS: Now I'll actually read it, and see if I have any thoughts. 🙂

      So, the API is returning a JSON string. Therefore, when you ask about using "decode", I'm going to assume you mean json_decode(), which can be used to convert that string into an array (or object). If so, then you simply need to run the output of your cURL request through it. For example:

      if ($err) {
          echo "cURL Error #:" . $err;
      } else {
          $response_array = json_decode($response, true); // I prefer `true` for everything as arrays, YMMV
          if($response_array === false) {
              echo "response was not valid JSON:\n'$response'";
          }
          else {
              // Here you can do whatever it is you need to do with that array. To see what it looks like:
              echo "Here's the array:\n".var_export($response_array, true);
          }
      }
      

        Thank you for the edit, and yes I am referring to json decode. So this is the result of the decode and yes it looks like it has more potential to end up the way I need it, but how? What would I do to pick out just the piece of that array that shows the price value up to X number of decimal places and put the currency label on the end? I'm just in a weird place because I know it's doable, but I don't know how to tell the code to do it.

        Displayed Result:
        array ( 'usd-coin' => array ( 'usd' => 0.99985500000000004927613872496294789016246795654296875, ), )

        What I'm trying to end up with:
        0.99985500 usd

        What puzzles me, even more, is that some prices will need to look like 0.xxxxxxxx, and others x,xxx.xx. So if it's a leading zero before the decimal point it would show 8 decimal places, but if it was a 1 or greater in front of the decimal point it would only need to show 2 decimal places.

        I'm starting to understand just how much I don't know and considering just paying someone at this point. lol

        Welcome to the wonderful world of floating-point arithmetic, and an object lesson in why it shouldn't be used for monetary quantities. (Basically, 0.99985500000000004927613872496294789016246795654296875 is the closest approximation to 0.99985500 possible with the limited number of bits available: there's a big red warning about this on the floating point numbers manual page).

        You've got a lot of requirements, so you'll need to do some programming to put all the pieces together, but you can at least get the number to display the way you're wanting by using the number_format function, something like

        if($usd_value < 1)
            $usd_display = number_format($usd_value, decimals: 8, decimal_separator: '.');
        else
            $usd_display = number_format($usd_value, decimals: 2, decimal_separator: '.', thousands_separator: ',');
        

        jcrimmel So if it's a leading zero before the decimal point it would show 8 decimal places, but if it was a 1 or greater in front of the decimal point it would only need to show 2 decimal places.

        You could do a little if/else logic:

        function format_amount($amount) {
            $decimals = $amount < 1 ? 8 : 2;
            return number_format($amount, $decimals);
        }
        // test:
        foreach([2.3456789, 1.00001, 1.0, 0.999997321, 2.39e-05] as $amount) {
            echo "Original: $amount, Formatted: ".format_amount($amount)."\n";
        }
        

        ...which outputs...

        Original: 2.3456789, Formatted: 2.35
        Original: 1.00001, Formatted: 1.00
        Original: 1, Formatted: 1.00
        Original: 0.999997321, Formatted: 0.99999732
        Original: 2.39E-5, Formatted: 0.00002390
        
          Write a Reply...