Jump to content

Formatting Data Returned in PHP API Call


jcrimmel

Recommended Posts

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

Link to comment
Share on other sites

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;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.