جزء المقارنة

PHOTO EMBED

Tue Jan 02 2024 00:37:16 GMT+0000 (Coordinated Universal Time)

Saved by @mebean

function getGoldPrices($apiKey, $symbol, $curr, $date = "") {
    $myHeaders = array(
        'x-access-token: ' . $apiKey,
        'Content-Type: application/json'
    );

    $curl = curl_init();
    $url = "https://www.goldapi.io/api/{$symbol}/{$curr}{$date}";

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTPHEADER => $myHeaders
    ));

    $response = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);

    if ($error) {
        // handle the error
        echo 'Error: ' . $error;
        return null;
    } else {
        $data = json_decode($response, true);

        if ($data !== null) {
            // Create an associative array to store the gold prices
            $goldPrices = array(
                'price_gram_24k' => $data['price_gram_24k'],
                'price_gram_22k' => $data['price_gram_22k'],
                'price_gram_21k' => $data['price_gram_21k'],
                'price_gram_20k' => $data['price_gram_20k'],
                'price_gram_18k' => $data['price_gram_18k'],
                'price_gram_16k' => $data['price_gram_16k'],
                'price_gram_14k' => $data['price_gram_14k'],
                'price_gram_10k' => $data['price_gram_10k'],
            );

            // Add the timestamp to the array
            $goldPrices['timestamp'] = $data['timestamp'];

            return $goldPrices;
        }

        return null;
    }
}

function getLastButOneRecord($apiKey, $symbol, $curr, $date = "") {
    $myHeaders = array(
        'x-access-token: ' . $apiKey,
        'Content-Type: application/json'
    );

    $curl = curl_init();
    $url = "https://www.goldapi.io/api/{$symbol}/{$curr}{$date}";

    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTPHEADER => $myHeaders
    ));

    $response = curl_exec($curl);
    $error = curl_error($curl);
    curl_close($curl);

    if ($error) {
        echo 'Error: ' . $error;
        return null;
    } else {
        $data = json_decode($response, true);

        if ($data !== null) {
            $goldPrices = array(
                'price_gram_24k' => $data['price_gram_24k'],
                'price_gram_22k' => $data['price_gram_22k'],
                'price_gram_21k' => $data['price_gram_21k'],
                'price_gram_20k' => $data['price_gram_20k'],
                'price_gram_18k' => $data['price_gram_18k'],
                'price_gram_16k' => $data['price_gram_16k'],
                'price_gram_14k' => $data['price_gram_14k'],
                'price_gram_10k' => $data['price_gram_10k'],
            );

            $goldPrices['timestamp'] = $data['timestamp'];

            $keys = array_keys($goldPrices);
            $secondToLastKey = $keys[count($keys) - 2];
            $secondToLastValue = $goldPrices[$secondToLastKey];

            return $secondToLastValue;
        }

        return null;
    }
}
content_copyCOPY